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("---------------------");
			}
		}
	}

這里使用了Dom4JselectNodes()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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM