package com.test.xml; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.dom4j.tree.DefaultDocument; public class BuildXml { public static void main(String[] args) { BuildXml xml=new BuildXml(); String fileName="xml/bu.xml"; String[] itemName = { "航空加油站", "纪检中心", "油库中心", "其他" }; String[] sheehCod = { "1.1.1", "1.1.2", "1.1.3" }; xml.TestXml(fileName,itemName,sheehCod); } public void TestXml(String fileName, String[] itemName, String[] sheehCod){ //建立doc对象 Document doc=DocumentHelper.createDocument(); //建立xml文档的Record根对象 Element recordElement=doc.addElement("Record"); //为Record根建立一个Head节点 Element headElement=recordElement.addElement("Head"); //为Record根建立一个body节点 Element bodyElement=recordElement.addElement("Body"); //为Head节点添加一些子节点 Element codEl=headElement.addElement("Cod"); codEl.setText("1"); Element recEl=headElement.addElement("Recitified"); recEl.setText("是"); /** 叶子节点编号*/ addParamList(bodyElement, sheehCod, itemName); //格式化输出xml文档,并解决中文问题 try { FileWriter fileWriter=new FileWriter(fileName); //设置文件编码格式 //OutputFormat.createPrettyPrint();设置了创建xml文件的格式为缩进的 OutputFormat xmlFormat=OutputFormat.createPrettyPrint(); //这里不知道为什么设置编码格式为utf-8就会出现xml里没有中文显示 ,谁知道请说下.谢谢 xmlFormat.setEncoding("gbk"); //创建写文件方法 XMLWriter xmlWriter = new XMLWriter(fileWriter,xmlFormat); //写入文件 xmlWriter.write(doc); //关闭 xmlWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void addParamList(Element bodyEl, String[] sheehCod, String[] itemName) { /**有多少个子节点就产生多少个对象*/ for (int i = 0; i < sheehCod.length; i++) { Element paramListEl = bodyEl.addElement("ParamList"); Element sheehEl = paramListEl.addElement("SheehCod"); sheehEl.setText(sheehCod[i]); //调用方法 addItem(itemName, paramListEl); } } private void addItem(String[] itemName, Element paramListEl) { Element paramEl = paramListEl.addElement("Param"); for (int i = 0; i < itemName.length; i++) { Element itemNameEl = paramEl.addElement("ItemName"); itemNameEl.setText(itemName[i]); if (i < itemName.length - 1) { paramEl = paramListEl.addElement("Param"); } } } } //代码运行产生XML文档如下: <?xml version="1.0" encoding="gbk"?> <Record> <Head> <Cod>1</Cod> <Recitified>是</Recitified> </Head> <Body> <ParamList> <SheehCod>1.1.1</SheehCod> <Param> <ItemName>航空加油站</ItemName> </Param> <Param> <ItemName>纪检中心</ItemName> </Param> <Param> <ItemName>油库中心</ItemName> </Param> <Param> <ItemName>其他</ItemName> </Param> </ParamList> <ParamList> <SheehCod>1.1.2</SheehCod> <Param> <ItemName>航空加油站</ItemName> </Param> <Param> <ItemName>纪检中心</ItemName> </Param> <Param> <ItemName>油库中心</ItemName> </Param> <Param> <ItemName>其他</ItemName> </Param> </ParamList> <ParamList> <SheehCod>1.1.3</SheehCod> <Param> <ItemName>航空加油站</ItemName> </Param> <Param> <ItemName>纪检中心</ItemName> </Param> <Param> <ItemName>油库中心</ItemName> </Param> <Param> <ItemName>其他</ItemName> </Param> </ParamList> </Body> </Record>