Xml 代码
1. <?xml version="1.0" encoding="GB2312"?>
2. <RESULT>
3. <VALUE>
4. <NO>A1234</NO>
5. <ADDR>河南省郑州市</ADDR>
6. </VALUE>
7. <VALUE>
8. <NO>B1234</NO>
9. <ADDR>河南省郑州市二七区</ADDR>
10. </VALUE>
11. </RESULT>
第一种 DOM 实现方法:
Java 代码
1. import java.io.File;
2.
3. import javax.xml.parsers.DocumentBuilder;
4. import javax.xml.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("data_10k.xml");
16. DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
17. DocumentBuilder builder = factory.newDocumentBuilder();
18. Document doc = builder.parse(f);
19. NodeList nl = doc.getElementsByTagName("VALUE");
20. for (int i = 0; i < nl.getLength(); i++) {
21. System.out.print("车牌号码:"+
doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue());
22. System.out.println("车主地址:"+
doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue(
));
23. }
24. } catch (Exception e) {
25. e.printStackTrace();