DOM4J解析實例 – JAVA編程語言程序開發技術文章

《D:\\wuhan\\test.xml》 
<?xml version="1.0" encoding="GB2312"?> 
<RESULT> 
    <VALUE><NO>A1234</NO><NO>A1234aaaaaaaaaa</NO><ADDR>深圳羅湖</ADDR> 
    </VALUE> 
    <VALUE>  
       <NO>B1234</NO>  
       <ADDR>深圳南山</ADDR> 
    </VALUE> 
    <VALUE>  
       <NO>B1234xxxx</NO>  
       <ADDR>深圳xxxxx</ADDR> 
    </VALUE> 
</RESULT> 
Java 代碼 
package com.xml; 
 
import java.io.*; 
import java.util.*; 
import org.dom4j.*; 
import org.dom4j.io.*;  
 
public class Dom4j {  
 
    public static void main(String arge[]) {  
        long lasting = System.currentTimeMillis();  
        try {  
            File f = new File("D:\\wuhan\\test.xml");  
            SAXReader reader = new SAXReader();  
            Document doc = reader.read(f);  
            Element root = doc.getRootElement();  
            Element foo;  
            for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) {  
                foo = (Element) i.next();  
                System.out.print("車牌號碼:" + foo.elementText("NO"));  
                System.out.println("車主地址:" + foo.elementText("ADDR"));  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    } 

 摘自  Java教程
 

發佈留言