第一 DOM 实现方法:
1. import java.io.File;   
2.  
3. import l.parsers.DocumentBuilder;   
4. import l.parsers.DocumentBuilderFactory;   
5.  
6. import org.w3c.dom.Document;   
7. import org.w3c.dom.NodeList;   
8.  
9. public class MyXMLReader2DOM {   
10. public static void main(String arge[]) {   
11.  
12.   long lasting = System.currentTimeMillis();   
13.  
14.   try {   
15.     File f = new File("l");   
16.     DocumentBuilderFactory factory = wInstance();   
17.     DocumentBuilder builder = wDocumentBuilder();   
18.     Document doc = builder.parse(f);   
19.     NodeList nl = ElementsByTagName("VALUE");   
20.    for (int i = 0; i < nl.getLength(); i++) {   
21.     System.out.print("车牌:"+ ElementsByTagName("NO").item(i).getFirstChild().getNodeValue());   
22.     System.out.println("主地址:"+ ElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue());   
23.     }   
24.   } catch (Exception e) {   
25.     e.printStackTrace();   
26.   }   
27. }   
28.
import java.io.File; l.parsers.DocumentBuilder; l.parsers.D
ocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; public class MyXMLReader2DOM { public static void main(String arge[]) { long lasting = System.currentTimeMillis(); try { File f = new File("l"); DocumentBuilderFactory factory = wInstance(); DocumentBuilder builder = wDocumentBuilder(); Document doc = builder.parse(f); NodeList nl = ElementsByTagName("VALUE"); for (int i = 0; i < nl.getLength(); i++) { System.out.print("牌号:"+ ElementsByTagName("NO").item(i).getFirstChild().getNodeValue()); System.out.println("主地址:"+ ElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue()); } } catch (Exception e) { e.printStackTrace(); } } }
第二DOM4J实现方法:
1. import java.io.*;   
2. import java.util.*;   
3. import org.dom4j.*;   
4. import org.dom4j.io.*;   
5.  
6. public class MyXMLReader2DOM4J {   
7. public static void main(String arge[]) {   
8.   long lasting = System.currentTimeMillis();   
9.   try {   
10.     File f = new File("l");   
11.     SAXReader reader = new SAXReader();   
12.     Document doc = ad(f);   
13.     Element root = RootElement();   
14.     Element foo;   
15.    for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) {   
16.     foo = (Element) i.next();   
17.     System.out.print("牌号:" + foo.elementText("NO"));   
18.     System.out.println("主地址:" + foo.elementText("ADDR"));   
19.     }   
20.   } catch (Exception e) {   
21.     e.printStackTrace();   
22.   }   
23. }   
24.
import java.io.*; import java.util.*; import org.dom4j.*; import org.dom4j.io.*; public class MyXMLReader2DOM4J { public static void main(String arge[]) { long lasting = System.currentTimeMillis(); try { File f = new File("l"); SAXReader reader = new SAXReader(); Document doc = ad(f); Element root = RootElement(); 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(); } } }
 
第三 JDOM实现方法:
1. import java.io.*;   
2. import java.util.*;   
3. import org.jdom.*;   
4. import org.jdom.input.*;   
5.  
6. public class MyXMLReader2JDOM {   
7. public static void main(String arge[]) {   
8.   long lasting = System.currentTimeMillis();   
9.   try {   
10.     SAXBuilder builder = new SAXBuilder();   
11.     Document doc = builder.build(new File("l"));   
12.     Element foo = RootElement();   
13.     List allChildren = Children();   
14.    for (int i = 0; i < allChildren.size(); i++) {   
15.     System.out.print("牌号:"+ ((Element) (i)).getChild("NO").getText());   
16.     System.out.println("主地址:"+ ((Element) (i)).getChild("ADDR").getText());   
17.     }   
18.   } catch (Exception e) {   
19.     e.printStackTrace();   
20.   }   
21. }   
22.
import java.io.*; import java.util.*; import org.jdom.*; import org.jdom.input.*; public class
MyXMLReader2JDOM { public static void main(String arge[]) { long lasting = System.currentTimeMillis(); try { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new File("l")); Element foo = RootElement(); List allChildren = Children(); for (int i = 0; i < allChildren.size(); i++) { System.out.print("牌号:"+ ((Element) (i)).getChild("NO").getText()); System.out.println("主地址:"+ ((Element) (i)).getChild("ADDR").getText()); } } catch (Exception e) { e.printStackTrace(); } } }
dom4j读取xml
 
 
 
  <?xml version="1.0" encoding="GB2312"?> 
  <RESULT> 
  <VALUE>     
    <NO>A1234</NO>     
    <ADDR>河南省州市</ADDR> 
  </VALUE> 
  <VALUE>     
    <NO>B1234</NO>     
    <ADDR>河南省州市二七区</ADDR> 
  </VALUE> 
  </RESULT> 

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