Java 生成xml文件並上傳

Maven項目導入依賴:前端

<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1</version>
</dependency>數據庫

根據前端傳過來的數據進行解析生成xml文件而且上傳。app

public class buildXml{dom

//String datas="{\"imei\":\"358732036143010\",\"version\":\"2.3\",\"apk\":[{\"appname\":\"SyncKey\",\"packagename\":\"com.discovery.synckey\",\"classname\":\"com.discovery.synckey.MainActivity\",\"bucketname\":\"dbtest.bucketname\",\"versioncode\":\"21\",\"versionname\":\"2.1\",\"objectkey\":\"SyncKey-release_v2.1.apk\",\"md5\":\"44459ac90e39e9d1822f20da19d3cfd5\",\"issilent\":\"true\",\"isrunning\":\"true\",\"type\":\"0\"},{\"appname\":\"DbTest\",\"packagename\":\"com.discovery.synckey\",\"classname\":\"com.discovery.synckey.MainActivity\",\"bucketname\":\"dbtest.bucketname\",\"versioncode\":\"21\",\"versionname\":\"2.1\",\"objectkey\":\"SyncKey-release_v2.1.apk\",\"md5\":\"44459ac90e39e9d1822f20da19d3cfd5\",\"issilent\":\"true\",\"isrunning\":\"true\",\"type\":\"0\"},{\"appname\":\"AppUpdate\",\"packagename\":\"com.discovery.synckey\",\"classname\":\"com.discovery.synckey.MainActivity\",\"bucketname\":\"dbtest.bucketname\",\"versioncode\":\"21\",\"versionname\":\"2.1\",\"objectkey\":\"SyncKey-release_v2.1.apk\",\"md5\":\"44459ac90e39e9d1822f20da19d3cfd5\",\"issilent\":\"true\",\"isrunning\":\"true\",\"type\":\"0\"}]}";
ui

 

String datas=(String)context.getValue("datas");
JSONObject reqJson=JSONObject.parseObject(datas);
String apk=reqJson.getString("apk");
String imei=reqJson.getString("imei");
System.out.println("imei+++++"+imei);
String version=reqJson.getString("version");
code

JSONArray parseArry =JSONObject.parseArray(apk);
System.out.println("傳過來的數據:");
System.out.println(parseArry);

Iterator<Object> iterator =parseArry.iterator();xml

System.out.println("!!!!!!!!");
// 建立根節點 list;
Element root = new Element("datas");

// 根節點添加到文檔中;
Document Doc = new Document(root);
Element versions = new Element("version").setText(version);
root.addContent(versions);
Element apks = new Element("apks");
root.addContent(apks);
// 此處 for 循環可替換成 遍歷 數據庫表的結果集操做;
// for (int i = 0; i < 3; i++) {
while(iterator.hasNext()){

JSONObject object =(JSONObject)iterator.next();
// 建立節點 user;
Element elements = new Element("apk");
// 給 user 節點添加屬性 id;
// elements.setAttribute("id", "" + i);

// 給 user 節點添加子節點並賦值;
// new Element("name")中的 "name" 替換成表中相應字段,setText("xuehui")中 "xuehui 替換成表中記錄值;
elements.addContent(new Element("appname").setText((String)object.getString("appname")));
elements.addContent(new Element("packagename").setText((String)object.getString("packagename")));
elements.addContent(new Element("classname").setText((String)object.getString("classname")));
elements.addContent(new Element("bucketname").setText((String)object.getString("bucketname")));
elements.addContent(new Element("versioncode").setText((String)object.getString("versioncode")));
elements.addContent(new Element("versionname").setText((String)object.getString("versionname")));
elements.addContent(new Element("objectkey").setText((String)object.getString("objectkey")));
elements.addContent(new Element("md5").setText((String)object.getString("MD5")));
elements.addContent(new Element("issilent").setText((String)object.getString("issilent")));
elements.addContent(new Element("isrunning").setText((String)object.getString("isrunning")));
elements.addContent(new Element("type").setText((String)object.getString("type")));
// 給父節點apks添加user子節點;

apks.addContent(elements);
}
XMLOutputter XMLOut = new XMLOutputter();
// 輸出 user.xml 文件;
String net="http://192.168.1.147:8090/xml/"; // 上傳對象存放的根目錄
String portal="C:/xml/";

UUID uuid = UUID.randomUUID();
String str = uuid + ".xml" ;
String netPath=net+str;
context.setValue("filePath", netPath);
context.setValue("fileType", "xml");
context.setValue("fileName", str);
context.setValue("imei", imei);
String fileNameString=portal+str;
File xml = new File(fileNameString);
System.out.println(fileNameString);
xml.createNewFile();
XMLOut.output(Doc, new FileOutputStream(xml));對象

}md5

相關文章
相關標籤/搜索