java读取xml文件的四种方法


Xml代码 复制代码
  1. <?xmlversion="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>

[xml] view plain copy
  1. <?xmlversion="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. import javax.xml.parsers.DocumentBuilder;  

  3. import javax.xml.parsers.DocumentBuilderFactory;  

  4. import org.w3c.dom.Document;  

  5. import org.w3c.dom.NodeList;  

  6. publicclass MyXMLReader2DOM {  

  7. publicstaticvoid main(String arge[]) {  

  8. long lasting = System.currentTimeMillis();  

  9. try {  

  10.   File f = new File("data_10k.xml");  

  11.   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  

  12.   DocumentBuilder builder = factory.newDocumentBuilder();  

  13.   Document doc = builder.parse(f);  

  14.   NodeList nl = doc.getElementsByTagName("VALUE");  

  15. for (int i = 0; i < nl.getLength(); i++) {  

  16.    System.out.print("车牌号码:"+ doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue());  

  17.    System.out.println("车主地址:"+ doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue());  

  18.   }  

  19.  } catch (Exception e) {  

  20.   e.printStackTrace();  

  21.  }  

  22. }  

  23. }  

[java] view plain copy
  1. import java.io.File;  

  2. import javax.xml.parsers.DocumentBuilder;  

  3. import javax.xml.parsers.DocumentBuilderFactory;  

  4. import org.w3c.dom.Document;  

  5. import org.w3c.dom.NodeList;  

  6. publicclass MyXMLReader2DOM {  

  7. publicstaticvoid main(String arge[]) {  

  8. long lasting = System.currentTimeMillis();  

  9. try {  

  10.   File f = new File("data_10k.xml");  

  11.   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  

  12.   DocumentBuilder builder = factory.newDocumentBuilder();  

  13.   Document doc = builder.parse(f);  

  14.   NodeList nl = doc.getElementsByTagName("VALUE");  

  15. for (int i = 0; i < nl.getLength(); i++) {  

  16.    System.out.print("车牌号码:"+ doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue());  

  17.    System.out.println("车主地址:"+ doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue());  

  18.   }  

  19.  } catch (Exception e) {  

  20.   e.printStackTrace();  

  21.  }  

  22. }  

  23. }  

第二种,DOM4J实现方法:

Java代码 复制代码
  1. import java.io.*;  

  2. import java.util.*;  

  3. import org.dom4j.*;  

  4. import org.dom4j.io.*;  

  5. publicclass MyXMLReader2DOM4J {  

  6. publicstaticvoid main(String arge[]) {  

  7. long lasting = System.currentTimeMillis();  

  8. try {  

  9.   File f = new File("data_10k.xml");  

  10.   SAXReader reader = new SAXReader();  

  11.   Document doc = reader.read(f);  

  12.   Element root = doc.getRootElement();  

  13.   Element foo;  

  14. for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) {  

  15.    foo = (Element) i.next();  

  16.    System.out.print("车牌号码:" + foo.elementText("NO"));  

  17.    System.out.println("车主地址:" + foo.elementText("ADDR"));  

  18.   }  

  19.  } catch (Exception e) {  

  20.   e.printStackTrace();  

  21.  }  

  22. }  

  23. }  

[java] view plain copy
  1. import java.io.*;  

  2. import java.util.*;  

  3. import org.dom4j.*;  

  4. import org.dom4j.io.*;  

  5. publicclass MyXMLReader2DOM4J {  

  6. publicstaticvoid main(String arge[]) {  

  7. long lasting = System.currentTimeMillis();  

  8. try {  

  9.   File f = new File("data_10k.xml");  

  10.   SAXReader reader = new SAXReader();  

  11.   Document doc = reader.read(f);  

  12.   Element root = doc.getRootElement();  

  13.   Element foo;  

  14. for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) {  

  15.    foo = (Element) i.next();  

  16.    System.out.print("车牌号码:" + foo.elementText("NO"));  

  17.    System.out.println("车主地址:" + foo.elementText("ADDR"));  

  18.   }  

  19.  } catch (Exception e) {  

  20.   e.printStackTrace();  

  21.  }  

  22. }  

  23. }  

第三种 JDOM实现方法:

Java代码 复制代码
  1. import java.io.*;  

  2. import java.util.*;  

  3. import org.jdom.*;  

  4. import org.jdom.input.*;  

  5. publicclass MyXMLReader2JDOM {  

  6. publicstaticvoid main(String arge[]) {  

  7. long lasting = System.currentTimeMillis();  

  8. try {  

  9.   SAXBuilder builder = new SAXBuilder();  

  10.   Document doc = builder.build(new File("data_10k.xml"));  

  11.   Element foo = doc.getRootElement();  

  12.   List allChildren = foo.getChildren();  

  13. for (int i = 0; i < allChildren.size(); i++) {  

  14.    System.out.print("车牌号码:"+ ((Element) allChildren.get(i)).getChild("NO").getText());  

  15.    System.out.println("车主地址:"+ ((Element) allChildren.get(i)).getChild("ADDR").getText());  

  16.   }  

  17.  } catch (Exception e) {  

  18.   e.printStackTrace();  

  19.  }  

  20. }  

  21. }  

[java] view plain copy
  1. import java.io.*;  

  2. import java.util.*;  

  3. import org.jdom.*;  

  4. import org.jdom.input.*;  

  5. publicclass MyXMLReader2JDOM {  

  6. publicstaticvoid main(String arge[]) {  

  7. long lasting = System.currentTimeMillis();  

  8. try {  

  9.   SAXBuilder builder = new SAXBuilder();  

  10.   Document doc = builder.build(new File("data_10k.xml"));  

  11.   Element foo = doc.getRootElement();  

  12.   List allChildren = foo.getChildren();  

  13. for (int i = 0; i < allChildren.size(); i++) {  

  14.    System.out.print("车牌号码:"+ ((Element) allChildren.get(i)).getChild("NO").getText());  

  15.    System.out.println("车主地址:"+ ((Element) allChildren.get(i)).getChild("ADDR").getText());  

  16.   }  

  17.  } catch (Exception e) {  

  18.   e.printStackTrace();  

  19.  }  

  20. }  

  21. }  


第四种SAX实现方法:

Java代码 复制代码
  1. import javax.xml.parsers.SAXParser;  

  2. import javax.xml.parsers.SAXParserFactory;  

  3. import org.xml.sax.Attributes;  

  4. import org.xml.sax.InputSource;  

  5. import org.xml.sax.SAXException;  

  6. import org.xml.sax.helpers.DefaultHandler;  

  7. publicclass MyXMLReader2SAX extends DefaultHandler {  

  8. java.util.Stack tags = new java.util.Stack();  

  9. public MyXMLReader2SAX() {  

  10. super();  

  11. }  

  12. publicstaticvoid main(String args[]) {  

  13. long lasting = System.currentTimeMillis();  

  14. try {  

  15.   SAXParserFactory sf = SAXParserFactory.newInstance();  

  16.   SAXParser sp = sf.newSAXParser();  

  17.   MyXMLReader2SAX reader = new MyXMLReader2SAX();  

  18.   sp.parse(new InputSource("data_10k.xml"), reader);  

  19.  } catch (Exception e) {  

  20.   e.printStackTrace();  

  21.  }  

  22.  System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)  

  23.    + "毫秒");  

  24. }  

  25. publicvoid characters(char ch[], int start, int length)  

  26. throws SAXException {  

  27.  String tag = (String) tags.peek();  

  28. if (tag.equals("NO")) {  

  29.   System.out.print("车牌号码:" + new String(ch, start, length));  

  30.  }  

  31. if (tag.equals("ADDR")) {  

  32.   System.out.println("地址:" + new String(ch, start, length));  

  33.  }  

  34. }  

  35. publicvoid startElement(String uri, String localName, String qName,  

  36.   Attributes attrs) {  

  37.  tags.push(qName);  

  38. }  

  39. }


你可能感兴趣的:(java,xml)