生成XML文件

前端之家收集整理的这篇文章主要介绍了生成XML文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

【1.标签里头没带属性

public void getXml(){
        try {
           //--------------创建xml文档开始--------------
           Document document = DocumentHelper.createDocument();
           Element eeeRoot,eee1,eee2;
           //创建根节点         eeeRoot
           eeeRoot = document.addElement("ROOT");
           eee1 = eeeRoot.addElement("SYSTEM");
           eee1.addElement("SYSTEM_ID").setText("<![CDATA[test]]>");
           eee1.addElement("CHECK_CODE").setText("<![CDATA[test]]>");
           eee1.addElement("SYS_ORGAN_ID").setText("<![CDATA[1]]>");
           eee2 = eeeRoot.addElement("REF") ;
           eee2.addElement("HOST_INFO_ID").setText("<![CDATA[123]]>"); 
           eee2.addElement("GUEST_INFO_ID").setText("<![CDATA[321]]>");
           String xmlUrl = "F:\\test\\";
           String fileName = xmlUrl+"param.xml";
           OutputFormat format = new OutputFormat("",true);
           format.setEncoding("GBK");
           XMLWriter writer = new XMLWriter(new FileOutputStream(fileName),format);
           writer.setEscapeText(false); //生成XML的时候,处理特殊字符 如‘<’、‘>’
           writer.write(document);
           writer.flush();
           writer.close();
        } catch (Exception ex) {
            ex.printStackTrace();
           System.out.println("文档创建异常---" + ex.getMessage());
       }
    }

生成的XML

<?xml version="1.0" encoding="GBK"?>
<root>
<system>
<system_id><![CDATA[test]]></system_id>
<check_code><![CDATA[test]]></check_code>
<sys_organ_id><![CDATA[1]]></sys_organ_id>
</system>
<ref>
<host_info_id><![CDATA[123]]></host_info_id>
<guest_info_id><![CDATA[321]]></guest_info_id>
</ref>
</root>

【2.标签里头有带属性

public void getXml(){
        try {
            //--------------创建xml文档开始--------------
            Document document = DocumentHelper.createDocument();
            Element eeeRoot,eee2;
            //创建根节点         eeeRoot
            eeeRoot = document.addElement("chart");
            eeeRoot.addAttribute("caption","项目统计表");
            eeeRoot.addAttribute("subcaption","");
            eeeRoot.addAttribute("lineThickness","1");
            eeeRoot.addAttribute("showValues","0");
            eeeRoot.addAttribute("formatNumberScale","0");
            eeeRoot.addAttribute("anchorRadius","2");
            eeeRoot.addAttribute("divLineAlpha","20");
            eeeRoot.addAttribute("divLineColor","CC3300"); 
            eeeRoot.addAttribute("bgAlpha","10,10");

            //创建categories节点-----------------start
            eee1 = eeeRoot.addElement("categories");
            //将categories节点插入chart节点中
            eee1.addElement("category").addAttribute("label","A");
            eee1.addElement("category").addAttribute("label","B");
            eee1.addElement("category").addAttribute("label","C");
            eee1.addElement("category").addAttribute("label","D");
            //创建categories节点-----------------end
            //创建dataset节点-----------------start
            eee2 = eeeRoot.addElement("dataset").addAttribute("seriesName","11").addAttribute("color","EB0130").addAttribute("anchorBorderColor","EB0130").addAttribute("anchorBgColor","EB0130");
            eee2.addElement("set").addAttribute("value","12");
            eee2.addElement("set").addAttribute("value","13");
            eee2.addElement("set").addAttribute("value","14");
            eee2.addElement("set").addAttribute("value","15");
            //创建dataset节点-----------------end
           String xmlUrl = "F:\\test\\";
           String fileName = xmlUrl+"test.xml";
           OutputFormat format = new OutputFormat("",true);
           format.setEncoding("gb2312");
           XMLWriter writer = new XMLWriter(new FileOutputStream(fileName),format);
          // writer.setEscapeText(false); //生成XML的时候,处理特殊字符 如‘<’、‘>’
           writer.write(document);
           writer.flush();
           writer.close();
        } catch (Exception ex) {
            ex.printStackTrace();
           System.out.println("文档创建异常---" + ex.getMessage());
       }

生成的XML

<?xml version="1.0" encoding="gb2312"?> 
<chart caption="项目统计表" subcaption="" lineThickness="1" showValues="0" formatNumberScale="0" anchorRadius="2" divLineAlpha="20" divLineColor="CC3300" bgAlpha="10,10">
<categories>
<category label="A"/>
<category label="B"/>
<category label="C"/>
<category label="D"/>
</categories>
<dataset seriesName="11" color="EB0130" anchorBorderColor="EB0130" anchorBgColor="EB0130">
<set value="12"/>
<set value="13"/>
<set value="14"/>
<set value="15"/>
</dataset>
</chart>
原文链接:https://www.f2er.com/xml/298456.html

猜你在找的XML相关文章