• java生成xml


    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保存路径
    String xmlPath = this.getClass().getClassLoader().getResource("../../").getPath().replace("api_zjg_port/", "")
    + "/zjgFile/messageXML/";//XML保存路径
    String billNo = "";
    String currTime = "";

    // 创建根节点 并设置它的属性 ;
    Element root = new Element("CAR_INFO");
    // 将根节点添加到文档中;
    Document Doc = new Document(root);
    // 创建节点 BILL_INFO;
    Element billInfoElems = new Element("BILL_INFO");
    // 给 BILL_INFO 节点添加子节点并赋值;
    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) {
    // 创建节点 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()) {
    // 给 CAR_DETAIL 节点添加子节点并赋值;
    carInfoElems.addContent(new Element(entry.getKey()).setText(detail.getString(entry.getKey())));
    }
    root.addContent(carInfoElems);
    }
    }
    Calendar Cld = Calendar.getInstance();
    int MI = Cld.get(Calendar.MILLISECOND);

    // 输出 books.xml 文件;
    // 使xml文件 缩进效果
    Format format = Format.getPrettyFormat();
    XMLOutputter XMLOut = new XMLOutputter(format);

    System.out.println("文件名称:" + xmlPath + billNo + "-" + currTime + MI + ".xml");
    XMLOut.output(Doc, new FileOutputStream(xmlPath + billNo + "-" + currTime + MI + ".xml"));
    System.out.println("文件已生成");

    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 {
    // 创建根节点 并设置它的属性 ;
    Element root = new Element("CAR_INFO");
    // 将根节点添加到文档中;
    Document Doc = new Document(root);
    // 创建节点 BILL_INFO;
    Element billInfoElems = new Element("BILL_INFO");
    // 给 BILL_INFO 节点添加子节点并赋值;
    billInfoElems.addContent(new Element("BUSI_TYPE").setText("BID_A"));
    billInfoElems.addContent(new Element("SHIP_NAME").setText("集装箱"));
    billInfoElems.addContent(new Element("VOYAGE_NO").setText("CZ"));
    billInfoElems.addContent(new Element("BILL_NO").setText("YMLUW190205735"));
    billInfoElems.addContent(new Element("WHARF_NAME").setText("张家港保税港务有限公司"));
    billInfoElems.addContent(new Element("SEND_TIME").setText("20170419151921"));
    root.addContent(billInfoElems);

    // 创建节点 CAR_DETAIL;
    Element carInfoElems = new Element("CAR_DETAIL");
    // 给 CAR_DETAIL 节点添加子节点并赋值;
    carInfoElems.addContent(new Element("VIN_NO").setText("5TFDY5F19HX616824"));
    carInfoElems.addContent(new Element("RFID").setText("888820160822000000003747"));
    carInfoElems.addContent(new Element("AREA_NO").setText("A23901"));
    carInfoElems.addContent(new Element("OCCUR_TIME").setText("20170419151905"));
    root.addContent(carInfoElems);
    // 输出 books.xml 文件;
    // 使xml文件 缩进效果
    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("文件已生成");

    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("正在生成 xml文件...");
    j2x.BuildXMLDoc();
    } catch (Exception e) {
    e.printStackTrace();
    }*/
    }
    }

  • 相关阅读:
    泛型
    Java反射及注解
    Java之线程与进程
    线程池
    HashMap、ConcurrentHashMap解析
    基于Opentracing+Jaeger全链路灰度调用链(转载)
    SpringBoot 开发案例之参数传递的正确姿势
    以np.concatenate为主题,谈谈numpy数组按维度合并的问题
    为什么阿里巴巴Java开发手册中强制要求整型包装类对象值用 equals 方法比较?
    [C++面试题]之字符串(转)
  • 原文地址:https://www.cnblogs.com/vofill/p/6953260.html
Copyright © 2020-2023  润新知