Java使用Dom4J 遍历复杂XML节点
public void analysisXML() throws DocumentException { SAXReader saxread = new SAXReader(); File xmlFile = new File("idcMonitor.xml"); if (xmlFile.exists()) { Document document = saxread.read(xmlFile);// 读取XML文件 List<Element> houseMonitorList = document.selectNodes("//idcMonitor/houseMonitor"); //找到位于idcMonitor下的houseMonitor节点 for (int i = 0; i < houseMonitorList.size(); i++) { Element houseMonitor=houseMonitorList.get(i); Element id=(Element) houseMonitor.selectSingleNode("id"); //获得houseMonitor节点下的id值 System.out.println(id.getName()+" "+id.getTextTrim()); Element ip=(Element) houseMonitor.selectSingleNode("ip"); //获得houseMonitor节点下的ip值 System.out.println(ip.getName()+" "+ip.getTextTrim()); Element port=(Element) houseMonitor.selectSingleNode("port"); //获得houseMonitor节点下的port值 System.out.println(port.getName()+" "+port.getTextTrim()); Element domain=(Element) houseMonitor.selectSingleNode("domain"); //获得houseMonitor节点下的domain值 System.out.println(domain.getName()+" "+domain.getTextTrim()); Element serviceType=(Element) houseMonitor.selectSingleNode("serviceType"); //获得houseMonitor节点下的serviceType值 System.out.println(serviceType.getName()+" "+serviceType.getTextTrim()); Element firstFound=(Element) houseMonitor.selectSingleNode("firstFound"); //获得houseMonitor节点下的firstFound值 System.out.println(firstFound.getName()+" "+firstFound.getTextTrim()); Element lastFound=(Element) houseMonitor.selectSingleNode("lastFound"); //获得houseMonitor节点下的lastFound值 System.out.println(lastFound.getName()+" "+lastFound.getTextTrim()); Element illegalInfo=(Element)houseMonitor.selectSingleNode("illegalInfo");//获得houseMonitor节点下的illegalInfo值 Iterator<Element> illegalInfoIterator=illegalInfo.elementIterator(); while(illegalInfoIterator.hasNext()) { Element temporary=illegalInfoIterator.next(); System.out.println(temporary.getName()+" "+temporary.getTextTrim()); } Element ipInfo=(Element)houseMonitor.selectSingleNode("ipInfo");//获得houseMonitor节点下的ipInfo值 Iterator<Element> ipInfoIterator=ipInfo.elementIterator(); while(ipInfoIterator.hasNext()) { Element temporary=ipInfoIterator.next(); System.out.println(temporary.getName()+" "+temporary.getTextTrim()); } System.out.println("---------------------"); } } }
这里使用了Dom4J的selectNodes()和selectSingleNode()还有迭代elementIterator()方法;
selectNodes()方法是指定节点,注意这后面的是有"s"的;
List<Element> houseMonitorList = document.selectNodes("//idcMonitor/houseMonitor"); //找到位于idcMonitor下的houseMonitor节点
selectSingleNode()方法是获得单个节点的,后上面的selectNodes()方法是相对的
Element illegalInfo=(Element)houseMonitor.selectSingleNode("illegalInfo");//获得houseMonitor节点下的illegalInfo值
elementIterator()方法是迭代这个节点下面的节点,但是如果这个迭代之后的节点还有节点,这个迭代器是不会进入的,也就是这个迭代器只会进入一层。
Element illegalInfo=(Element)houseMonitor.selectSingleNode("illegalInfo");//获得houseMonitor节点下的illegalInfo值 Iterator<Element> illegalInfoIterator=illegalInfo.elementIterator(); while(illegalInfoIterator.hasNext()) { Element temporary=illegalInfoIterator.next(); System.out.println(temporary.getName()+" "+temporary.getTextTrim()); }
最后看看上面执行的结果:
id 123 ip 10.2.45.123 port 100 domain www.sina.com serviceType 1 firstFound 2013-08-21 12:33:22 lastFound 2013-08-21 13:00:00 illegalType 1 currentState 1 user admin icpError 0 regError 1 regDomain www.baidu.com --------------------- id 456 ip 10.2.45.456 port 200 domain www.baidu.com serviceType 3 firstFound 2013-08-29 12:33:22 lastFound 2013-08-30 13:00:00 illegalType 2 currentState 2 user admin2 icpError 1 regError 2 regDomain www.ambimmort.com ---------------------
当然最后附上原始解析的XML:
<?xml version="1.0" encoding="UTF-8"?> <idcMonitor> <commandId>201020000000169282</commandId> <idcId>1019</idcId> <monitorState>1</monitorState> <houseMonitor> <id>123</id> <ip>10.2.45.123</ip> <port>100</port> <domain>www.sina.com</domain> <serviceType>1</serviceType> <firstFound>2013-08-21 12:33:22</firstFound> <lastFound>2013-08-21 13:00:00</lastFound> <illegalInfo> <illegalType>1</illegalType> <currentState>1</currentState> <user>admin</user> </illegalInfo> <ipInfo> <icpError>0</icpError> <regError>1</regError> <regDomain>www.baidu.com</regDomain> </ipInfo> </houseMonitor> <houseMonitor> <id>456</id> <ip>10.2.45.456</ip> <port>200</port> <domain>www.baidu.com</domain> <serviceType>3</serviceType> <firstFound>2013-08-29 12:33:22</firstFound> <lastFound>2013-08-30 13:00:00</lastFound> <illegalInfo> <illegalType>2</illegalType> <currentState>2</currentState> <user>admin2</user> </illegalInfo> <ipInfo> <icpError>1</icpError> <regError>2</regError> <regDomain>www.ambimmort.com</regDomain> </ipInfo> </houseMonitor> <timeStamp>2013-08-22 12:57:13</timeStamp> </idcMonitor>