java循环xml节点_Java使⽤Dom4J遍历复杂XML节点
public void analysisXML() throws DocumentException {
SAXReader saxread = new SAXReader();
File xmlFile = new File("l");
if (ists()) {
Document document = ad(xmlFile);// 读取XML⽂件
List houseMonitorList = document.selectNodes("//idcMonitor/houseMonitor"); //到位于idcMonitor下的houseMonitor节点for (int i = 0; i < houseMonitorList.size(); i++) {
Element (i);
Element id=(Element) houseMonitor.selectSingleNode("id"); //获得houseMonitor节点下的id值
System.out.Name()+" "+id.getTextTrim());
Element ip=(Element) houseMonitor.selectSingleNode("ip"); //获得houseMonitor节点下的ip值
System.out.Name()+" "+ip.getTextTrim());
Element port=(Element) houseMonitor.selectSingleNode("port"); //获得houseMonitor节点下的port值
System.out.Name()+" "+TextTrim());
Element domain=(Element) houseMonitor.selectSingleNode("domain"); //获得houseMonitor节点下的domain值
System.out.Name()+" "+TextTrim());
Element serviceType=(Element) houseMonitor.selectSingleNode("serviceType"); //获得houseMonitor节点下的serviceType值System.out.Name()+" "+TextTrim());
Element firstFound=(Element) houseMonitor.selectSingleNode("firstFound"); //获得houseMonitor节点下的firstFound值System.out.Name()+" "+TextTrim());
Element lastFound=(Element) houseMonitor.selectSingleNode("lastFound"); //获得houseMonitor节点下的lastFound值System.out.Name()+" "+TextTrim());
Element illegalInfo=(Element)houseMonitor.selectSingleNode("illegalInfo");//获得houseMonitor节点下的illegalInfo值
Iterator illegalInfoIterator=illegalInfo.elementIterator();
while(illegalInfoIterator.hasNext())
{
Element ();
System.out.Name()+" "+TextTrim());
}
Element ipInfo=(Element)houseMonitor.selectSingleNode("ipInfo");//获得houseMonitor节点下的ipInfo值
Iterator ipInfoIterator=ipInfo.elementIterator();
while(ipInfoIterator.hasNext())
{
Element ();
System.out.Name()+" "+TextTrim());
}
System.out.println("---------------------");
}
}
}
这⾥使⽤了Dom4J的selectNodes()和selectSingleNode()还有迭代elementIterator()⽅法;
selectNodes()⽅法是指定节点,注意这后⾯的是有"s"的;
List 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 illegalInfoIterator=illegalInfo.elementIterator();
while(illegalInfoIterator.hasNext())
{
Element ();
System.out.Name()+" "+TextTrim());
}
最后看看上⾯执⾏的结果:
id 123
ip 10.2.45.123
port 100
domain www.sina
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
---------------------
id 456
ip 10.2.45.456
port 200
domain www.baidu 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 ---------------------
当然最后附上原始解析的XML:201020000000169282
1019
1使用dom4j解析xml文件
123
10.2.45.123
100
www.sina
1
2013-08-21 12:33:22
2013-08-21 13:00:00
1
1
admin
1
www.baidu
456
10.2.45.456
200
www.baidu
3
2013-08-29 12:33:22 2013-08-30 13:00:00 2
2
admin2
1
2
www.ambimmort 2013-08-22 12:57:13

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。