四种xml操作方式的基本使用方法

    1. <?xml version="1.0" encoding="GB2312"?>
    2. <RESULT>
    3. <VALUE>
    4. <NO>A1234</NO>
    5. <ADDR>四川省XX县XX镇XX路X段XX号</ADDR>
    6. </VALUE>
    7. <VALUE>
    8. <NO>B1234</NO>
    9. <ADDR>四川省XX市XX乡XX村XX组</ADDR>
    10. </VALUE>
    11. </RESULT>
    1. 1DOM
    2. import java.io.*;
    3. import java.util.*;
    4. import org.w3c.dom.*;
    5. import javax.xml.parsers.*;
    6. public class MyXMLReader{
    7.  public static void 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;inl.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. 2SAX
    23. import org.xml.sax.*;
    24. import org.xml.sax.helpers.*;
    25. import javax.xml.parsers.*;
    26. public class MyXMLReader extends DefaultHandler {
    27.  java.util.Stack tags = new java.util.Stack();
    28.  public MyXMLReader() {
    29.   super();
    30. }
    31.  public static void main(String args[]) {
    32.   long lasting = System.currentTimeMillis();
    33.   try {
    34.    SAXParserFactory sf = SAXParserFactory.newInstance();
    35.    SAXParser sp = sf.newSAXParser();
    36.    MyXMLReader reader = new MyXMLReader();
    37.    sp.parse(new InputSource("data_10k.xml"), reader);
    38.   } catch (Exception e) {
    39.    e.printStackTrace();
    40.   }
    41.   System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + "毫秒");}
    42.   public void characters(char ch[], int start, int length) throws SAXException {
    43.   String tag = (String) tags.peek();
    44.   if (tag.equals("NO")) {
    45.    System.out.print("车牌号码:" + new String(ch, start, length));
    46. }
    47. if (tag.equals("ADDR")) {
    48.   System.out.println("地址:" + new String(ch, start, length));
    49. }
    50. }
    51.   public void startElement(String uri,String localName,String qName,Attributes attrs) {
    52.   tags.push(qName);}
    53. }
    54. 3 JDOM
    55. import java.io.*;
    56. import java.util.*;
    57. import org.jdom.*;
    58. import org.jdom.input.*;
    59. public class MyXMLReader {
    60.  public static void main(String arge[]) {
    61.   long lasting = System.currentTimeMillis();
    62.   try {
    63.    SAXBuilder builder = new SAXBuilder();
    64.    Document doc = builder.build(new File("data_10k.xml"));
    65.    Element foo = doc.getRootElement();
    66.    List allChildren = foo.getChildren();
    67.    for(int i=0;iallChildren.size();i++) {
    68.     System.out.print("车牌号码:" + ((Element)allChildren.get(i)).getChild("NO").getText());
    69.     System.out.println("车主地址:" + ((Element)allChildren.get(i)).getChild("ADDR").getText());
    70.    }
    71.   } catch (Exception e) {
    72.    e.printStackTrace();
    73. }
    74. }
    75. 4DOM4J
    76. import java.io.*;
    77. import java.util.*;
    78. import org.dom4j.*;
    79. import org.dom4j.io.*;
    80. public class MyXMLReader {
    81.  public static void main(String arge[]) {
    82.   long lasting = System.currentTimeMillis();
    83.   try {
    84.    File f = new File("data_10k.xml");
    85.    SAXReader reader = new SAXReader();
    86.    Document doc = reader.read(f);
    87.    Element root = doc.getRootElement();
    88.    Element foo;
    89.    for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) {
    90.     foo = (Element) i.next();
    91.     System.out.print("车牌号码:" + foo.elementText("NO"));
    92.     System.out.println("车主地址:" + foo.elementText("ADDR"));
    93.    }
    94.   } catch (Exception e) {
    95.    e.printStackTrace();
    96. }
    97. }