Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date;
Import Java.util.Map.Entry;
Import org.jdom2.Document;
Import org.jdom2.Element;
Import Org.jdom2.output.Format;
Import Org.jdom2.output.XMLOutputter;
Import Com.alibaba.fastjson.JSONArray;
Import Com.alibaba.fastjson.JSONObject;
Import Com.wrt.zjg.webservices.model.FtpModel;
public class Javatoxml {
public void Buildjsontoxmldoc (Jsonobject resmain) throws Exception {
String Xmlpath = "e:/test/";//xml save path
String Xmlpath = This.getclass (). getClassLoader (). GetResource (".. /.. /"). GetPath (). Replace (" api_zjg_port/"," "")
+ "/zjgfile/messagexml/";//xml save path
String Billno = "";
String currtime = "";
Create a root node and set its properties;
element root = new Element ("Car_info");
Add the root node to the document;
Document DOC = new document (root);
Create node Bill_info;
Element billinfoelems = new Element ("Bill_info");
Add a child node to the Bill_info node and assign a value;
System.out.println (Resmain.tostring ());
String carinfo = resmain.getstring ("Car_info");
Jsonobject carinfoobj = Jsonobject.parseobject (Carinfo);
String billinfo = carinfoobj.getstring ("Bill_info");
Jsonobject billinfoobj = Jsonobject.parseobject (Billinfo);
For (entry<string, object> entry:billInfoObj.entrySet ()) {
Billinfoelems.addcontent (New Element (Entry.getkey ()). SetText (Billinfoobj.getstring (Entry.getkey ()));
if (Entry.getkey (). Equals ("Bill_no")) {
Billno = billinfoobj.getstring ("Bill_no");
}
if (Entry.getkey (). Equals ("Send_time")) {
Currtime = billinfoobj.getstring ("Send_time");
}
}
Root.addcontent (Billinfoelems);
String Cardetail = carinfoobj.getstring ("Car_detail");
Jsonarray Cardetailarr = Jsonarray.parsearray (Cardetail);
if (cardetailarr.size () > 0) {
Create node Car_detail;
for (int i = 0; i < cardetailarr.size (); i++) {
Jsonobject detail = Cardetailarr.getjsonobject (i);
Element carinfoelems = new Element ("Car_detail");
For (entry<string, object> entry:detail.entrySet ()) {
Add a child node to the Car_detail node and assign a value;
Carinfoelems.addcontent (New Element (Entry.getkey ()). SetText (Detail.getstring (Entry.getkey ()));
}
Root.addcontent (Carinfoelems);
}
}
Calendar Cld = Calendar.getinstance ();
int MI = Cld.get (Calendar.millisecond);
Output books.xml file;
Make XML file Indent effect
Format format = Format.getprettyformat ();
Xmloutputter xmlout = new Xmloutputter (format);
System.out.println ("File name:" + xmlpath + Billno + "-" + currtime + MI + ". xml");
Xmlout.output (Doc, New FileOutputStream (Xmlpath + Billno + "-" + currtime + MI + ". xml"));
SYSTEM.OUT.PRINTLN ("File has been generated");
Ftpmodel fm = new Ftpmodel ();
FM.SETIPADDR ("192.168.1.252");
Fm.setusername ("ftp");
Fm.setpwd ("ftp12345");
Fm.setpath ("/temp/zjg/messagexml/");
FTPUTIL.CONNECTFTP (FM);
File File = new file (Xmlpath + Billno + "-" + currtime + MI + ". xml");
Ftputil.upload (file);
}
public void Buildxmldoc () throws Exception {
Create a root node and set its properties;
element root = new Element ("Car_info");
Add the root node to the document;
Document DOC = new document (root);
Create node Bill_info;
Element billinfoelems = new Element ("Bill_info");
Add a child node to the Bill_info node and assign a value;
Billinfoelems.addcontent (New Element ("Busi_type"). SetText ("bid_a"));
Billinfoelems.addcontent (New Element ("Ship_name"). SetText ("container"));
Billinfoelems.addcontent (New Element ("Voyage_no"). SetText ("CZ"));
Billinfoelems.addcontent (New Element ("Bill_no"). SetText ("YMLUW190205735"));
Billinfoelems.addcontent ("Wharf_name"). SetText ("Zhangjiagang bonded Port Co., Ltd.");
Billinfoelems.addcontent (New Element ("Send_time"). SetText ("20170419151921"));
Root.addcontent (Billinfoelems);
Create node Car_detail;
Element carinfoelems = new Element ("Car_detail");
Add a child node to the Car_detail node and assign a value;
Carinfoelems.addcontent (New Element ("Vin_no"). SetText ("5tfdy5f19hx616824"));
Carinfoelems.addcontent (The New Element ("RFID"). SetText ("888820160822000000003747"));
Carinfoelems.addcontent (New Element ("Area_no"). SetText ("A23901"));
Carinfoelems.addcontent (New Element ("Occur_time"). SetText ("20170419151905"));
Root.addcontent (Carinfoelems);
Output books.xml file;
Make XML file Indent effect
Format format = Format.getprettyformat ();
Xmloutputter xmlout = new Xmloutputter (format);
Calendar cal = Calendar.getinstance ();
Date date = Cal.gettime ();
SimpleDateFormat Sdformat = new SimpleDateFormat ("Yyyymmddhhmmsssss");
String myTime = Sdformat.format (date);
Xmlout.output (Doc, New FileOutputStream ("e:/test/5tfdy5f19hx616824-" + MyTime + ". xml"));
SYSTEM.OUT.PRINTLN ("File has been generated");
Ftpmodel fm = new Ftpmodel ();
FM.SETIPADDR ("192.168.1.252");
Fm.setusername ("ftp");
Fm.setpwd ("ftp12345");
Fm.setpath ("/temp/zjg/messagexml/");
FTPUTIL.CONNECTFTP (FM);
File File = new file ("e:/test/5tfdy5f19hx616824-" + MyTime + ". xml");
Ftputil.upload (file);
}
public static void Main (string[] args) {
/*try {
Javatoxml j2x = new Javatoxml ();
SYSTEM.OUT.PRINTLN ("Generating XML File ...");
J2x. Buildxmldoc ();
} catch (Exception e) {
E.printstacktrace ();
}*/
}
}
Java generates XML