Java DOM4J读取XML

DOM4J是dom4j.org出品的一个开源XML解析包。Dom4j是一个易用的、开源的库,用于XML,XPath和XSLT。它应用于Java平台,采用了Java集合框架并完全支持DOM,SAX和JAXP。Hibernate也是用它来读写配置文件的。  
  
一、DOM4j的下载  
dom4j-1.6.1.jar 下载:http://sourceforge.net/directory/os:windows/freshness:recently-updated/?q=dom4j  
  
二、DOM4j解析XML文件
AirPortLine.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>    
<AirPortLine>    
    <line lid="MU2480" num="3">    
        <id>1</id>    
        <station>    
            <sid>1</sid>    
            <sname>武汉</sname>    
        </station>    
        <station>    
            <sid>2</sid>    
            <sname>石家庄</sname>    
        </station>    
        <station>    
            <sid>3</sid>    
            <sname>北京</sname>    
        </station>    
    </line>    
    <line lid="MU2483" num="4">    
        <id>2</id>    
        <station>    
            <sid>1</sid>    
            <sname>重庆</sname>    
        </station>    
        <station>    
            <sid>2</sid>    
            <sname>武汉</sname>    
        </station>    
        <station>    
            <sid>3</sid>    
            <sname>上海</sname>    
        </station>    
        <station>    
            <sid>4</sid>    
            <sname>东京</sname>    
        </station>    
    </line>    
    <line lid="MU2485" num="2">    
        <id>3</id>    
        <station>    
            <sid>1</sid>    
            <sname>上海</sname>    
        </station>    
        <station>    
            <sid>2</sid>    
            <sname>纽约</sname>    
        </station>    
    </line>    
</AirPortLine>  
Java DOM4J解析XML代码如下:
package com.hsinghsu.testXML;  
  
import java.io.File;  
import java.util.Iterator;  
  
import org.dom4j.Attribute;  
import org.dom4j.Document;  
import org.dom4j.DocumentException;  
import org.dom4j.DocumentHelper;  
import org.dom4j.Element;  
import org.dom4j.io.SAXReader;  
  
public class testDOM4J {  
  
    public static void main(String[] args) {  
  
        try {  
            File file = new File("AirPortLine.xml");  
            SAXReader reader = new SAXReader();  
            Document doc = reader.read(file);  
  
            parseDOM4J(doc);// 解析XML文档  
  
            //String xmlStr = getStringFromXML(doc);// 将XML文档转换为String  
            //System.out.println("xmlStr:" + xmlStr);  
  
            //String str = "<?xml version='1.0' encoding='UTF-8'?><AirPortLine><line lid='MU2480' num='3'><id>1</id><station><sid>1</sid><sname>武汉</sname></station><station><sid>2</sid><sname>石家庄</sname></station><station><sid>3</sid><sname>北京</sname></station></line><line lid='MU2482' num='4'><id>2</id><station><sid>1</sid><sname>重庆</sname></station><station><sid>2</sid><sname>武汉</sname></station><station><sid>3</sid><sname>上海</sname></station><station><sid>4</sid><sname>东京</sname></station></line><line lid='MU2483' num='2'><id>3</id><station><sid>1</sid><sname>上海</sname></station><station><sid>2</sid><sname>纽约</sname></station></line></AirPortLine>";  
            //Document document = getXMLDocFromString(str);// 将String转换为XML文档  
  
        } catch (Exception e) {  
            System.out.println("Can't read the file");  
        }  
    } 

	// 解析XML文档  
	private static void parseDOM4J(Document doc) {  
	    Element root = doc.getRootElement();  
	    for (Iterator iter = root.elementIterator("line"); iter.hasNext();) {// 遍历line结点的所有子节点,也可以使用root.elementIterator()  
	        Element element = (Element) iter.next();  
	  
	        Attribute lidAttr = element.attribute("lid");// 获取<line>元素的属性  
	        String lid = lidAttr.getValue();// 获取<line>元素的属性值  
	        Attribute numAttr = element.attribute("num");// 获取<line>元素的属性  
	        String num = numAttr.getValue();// 获取<line>元素的属性值  
	  
	        String id = element.elementText("id");// 获取<line>元素下<id></id>的值  
	  
	        System.out.println("==lid:" + lid);  
	        System.out.println("==num:" + num);  
	        System.out.println("==路线id:" + id);  
	  
	        for (Iterator iterInner = element.elementIterator("station"); iterInner.hasNext();) { // 遍历station结点的所有子节点  
	            Element elementInner = (Element) iterInner.next();  
	  
	            String sid = elementInner.elementText("sid");// 获取<station>元素下<sid></sid>的值  
	            String sname = elementInner.elementText("sname");// 获取<station>元素下<sname></sname>的值  
	  
	            System.out.println("----站--sid:" + sid);  
	            System.out.println("----站--sname:" + sname);  
	        }  
	  
	    }  
	}  
  
	// 将XML文档转换为String  
	private static String getStringFromXML(Document doc) {  
	    return doc.asXML();  
	}  
	  
	// 将String转换为XML文档  
	private static Document getXMLDocFromString(String str) {  
	    try {  
	        return DocumentHelper.parseText(str);  
	    } catch (DocumentException e) {  
	        e.printStackTrace();  
	        return null;  
	    }  
  
}  

你可能感兴趣的:(Java DOM4J读取XML)