1. 程式人生 > >【Java】解析xml

【Java】解析xml

tle void code public cnblogs () clas 河南 asn

xml:

技術分享
<?xml version="1.0" encoding="GB2312"?>  
<RESULT>  
<VALUE>     
  <NO>A1234</NO>     
  <ADDR>河南省鄭州市</ADDR>  
</VALUE>  
<VALUE>     
  <NO>B1234</NO>     
  <ADDR>河南省鄭州市二七區</ADDR>  
</VALUE>  
</RESULT>
技術分享

代碼:

package parXml;
 
import java.io.File;
import java.util.Iterator;
 
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
public class parxml {
    public static void main(String[] argc) {
 
        try {
            File f = new File("data.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.println(foo.elementText(
"ADDR")); } } catch (Exception e) { e.printStackTrace(); } } }

【Java】解析xml