以下代码分为2部分:
第一部分 是上边黑色部分代码(即66行之前),完成的功能是读取XML中的内容,并将值保存到按XML的逻辑关系建成的类中。即将每个XML保存成一个类的实例。这里也体现了如何逐层解析XML文档。
第二部分 是下边的蓝色部分代码(即66行之后),完成的功能是将通过第一步得到的类的实例写入到一个XML文件中。这里体现了如何写XML。
具体代码如下:
1 import java.io.File; 2 import java.io.FileWriter; 3 import java.io.IOException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.dom4j.Document; 8 import org.dom4j.DocumentException; 9 import org.dom4j.DocumentHelper; 10 import org.dom4j.Element; 11 import org.dom4j.io.OutputFormat; 12 import org.dom4j.io.SAXReader; 13 import org.dom4j.io.XMLWriter; 14 15 16 public class HandleXML { 17 18 19 public static void main(String[] args) { 20 // TODO Auto-generated method stub 21 22 SAXReader reader = new SAXReader(); 23 reader.setEncoding("gb2312"); 24 Document document; 25 List serviceSystems = new ArrayList<ServiceSystem>(); 26 List list1,list2,list3; 27 List serverList = new ArrayList(); 28 List ServerTypeList = new ArrayList(); 29 List ServiceSystemList = new ArrayList(); 30 try { 31 document = reader.read(new File("E:\\serviceSystem.xml")); 32 Element rootElm = document.getRootElement(); 33 list1 = (List)rootElm.elements(); 34 for(Object oj0:list1){ 35 Element element1 = (Element) oj0; 36 ServiceSystem s = new ServiceSystem(); 37 s.systemName = element1.element("systemName").getText(); 38 list2 = new ArrayList<ServerType>(); 39 list2 = element1.elements("type"); 40 for(Object oj1:list2){ 41 Element element2 = (Element)oj1; 42 ServerType type = new ServerType(); 43 type.typeName = element2.element("typeName").getText(); 44 list3 = (List)element2.elements("server"); 45 for(Object oj:list3){ 46 Element element3 = (Element)oj; 47 Server server = new Server(); 48 server.serverName = element3.element("serverName").getText(); 49 server.vmName = element3.element("vmName").getText(); 50 server.desc = element3.element("desc").getText(); 51 serverList.add(server); 52 type.servers.add(server); 53 } 54 55 56 s.serverTypes.add(type); 57 } 58 ServiceSystemList.add(s); 59 } 60 serviceSystems = ServiceSystemList; 61 } catch (DocumentException e) { 62 // TODO Auto-generated catch block 63 e.printStackTrace(); 64 } 65 66 XMLWriter writer = null;// 声明写XML的对象 67 68 OutputFormat format = OutputFormat.createPrettyPrint(); 69 format.setEncoding("gb2312");// 设置XML文件的编码格式 70 71 String filePath = "E:\\s.xml"; 72 File file = new File(filePath); 73 74 Document _document = DocumentHelper.createDocument(); 75 Element _root = _document.addElement("serviceSystems"); 76 77 for(Object oj:serviceSystems){ 78 ServiceSystem s = (ServiceSystem)oj; 79 Element _serviceSystem = _root.addElement("serviceSystem"); 80 List l1 = s.getServerTypes(); 81 Element _systemName = _serviceSystem.addElement("systemName"); 82 _systemName.setText(s.getSystemName()); 83 84 for(Object oj1:l1){ 85 Element _type = _serviceSystem.addElement("type"); 86 ServerType st = (ServerType)oj1; 87 List l2 = st.getServers(); 88 Element _typeName = _type.addElement("typeName"); 89 _typeName.setText(st.getTypeName()); 90 91 for(Object oj2:l2){ 92 Element _server = _type.addElement("server"); 93 Server ss = (Server)oj2; 94 Element _serverName = _server.addElement("serverName"); 95 _serverName.setText(ss.getServerName()); 96 Element _vmName = _server.addElement("vmName"); 97 _vmName.setText(ss.getVmName()); 98 Element _desc = _server.addElement("desc"); 99 _desc.setText(ss.getDesc()); 100 } 101 } 102 } 103 104 try { 105 writer = new XMLWriter(new FileWriter(file), format); 106 writer.write(_document); 107 writer.close(); 108 } catch (IOException e) { 109 // TODO Auto-generated catch block 110 e.printStackTrace(); 111 } 112 113 } 114 115 116 117 118 }