
正文
命令运行带参数的jar
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
一.打包(此处用的是eclipse)
代码如下,此如引用了某博主的代码,因忘记地址,如博主发现此文,可私信我
package com.example.Open;
import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader; import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class XmlToJson { /**
*
* @author: jinzy
* @date: 2018年7月18日下午5:39:20
* @description: 获取xml文件
* @param xmlName
* @return 设定文件
* Document 返回类型
*/
public static Document getXml(String xmlName) {
String fileDir = System.getProperty("user.dir");
String xmlFile = fileDir + "/reports/"+xmlName+".xml";
System.out.println("xmlFile:"+xmlFile);
File file = new File(xmlFile);
SAXReader reader = new SAXReader();
Document doc = null;
try {
doc = reader.read(file);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return doc;
} /**
*
* @author: jinzy
* @date: 2018年7月18日下午5:40:01
* @description: 将xml转换为JSON对象
* @param xmlName
* @return
* @throws Exception 设定文件
* JSONObject 返回类型
*/
public static JSONObject xmltoJson(String xmlName) throws Exception {
JSONObject jsonObject = new JSONObject();
//获取根节点元素对象
Document document = getXml(xmlName);
Element root = document.getRootElement();
iterateNodes(root, jsonObject);
return jsonObject;
} /**
* 遍历元素
* @param node 元素
* @param json 将元素遍历完成之后放的JSON对象
*/
public static void iterateNodes(Element node,JSONObject json){
//获取当前元素的名称
String nodeName = node.getName();
//判断已遍历的JSON中是否已经有了该元素的名称
if(json.containsKey(nodeName)){
//该元素在同级下有多个
Object Object = json.get(nodeName);
JSONArray array = null;
if(Object instanceof JSONArray){
array = (JSONArray) Object;
}else {
array = new JSONArray();
array.add(Object);
}
//获取该元素下所有子元素
List<Element> listElement = node.elements();
if(listElement.isEmpty()){
//该元素无子元素,获取元素的值
String nodeValue = node.getTextTrim();
array.add(nodeValue);
json.put(nodeName, array);
return ;
}
//有子元素
JSONObject newJson = new JSONObject();
//遍历所有子元素
for(Element e:listElement){
//递归
iterateNodes(e,newJson);
}
array.add(newJson);
json.put(nodeName, array);
return ;
}
//该元素同级下第一次遍历
//获取该元素下所有子元素
List<Element> listElement = node.elements();
if(listElement.isEmpty()){
//该元素无子元素,获取元素的值
String nodeValue = node.getTextTrim();
json.put(nodeName, nodeValue);
return ;
}
//有子节点,新建一个JSONObject来存储该节点下子节点的值
JSONObject object = new JSONObject();
//遍历所有一级子节点
for(Element e:listElement){
//递归
iterateNodes(e,object);
}
json.put(nodeName, object); } /**
* 测试的main方法
*/
public static void main(String[] args) throws Exception {
JSONObject jsonObject = xmltoJson(args[0]);
System.out.println(jsonObject);
}
}
(1)工程名右击,选择“Export”

(2)选择“Runnable JAR file”

(3)选择“Launch configuration”和jar要存放的路径

(4)点击“Finish”
二:运行
uacProdResult为参数,多个参数用空格隔开









