乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      java讀取xml文件的四種方法

       女巫城堡 2012-07-24
      java讀取xml文件的四種方法
      2009-08-25 10:42

      Xml代碼 復(fù)制代碼

      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>河南省鄭州市二七區(qū)</ADDR>  
      10. </VALUE>  
      11. </RESULT>   

      第一種 DOM 實(shí)現(xiàn)方法:

      Java代碼 復(fù)制代碼
      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("車牌號(hào)碼:"+ 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();   
      26.    }   
      27. }   
      28. }  

      第二種,DOM4J實(shí)現(xiàn)方法:

      Java代碼 復(fù)制代碼
      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("data_10k.xml");   
      11.     SAXReader reader = new SAXReader();   
      12.     Document doc = reader.read(f);   
      13.     Element root = doc.getRootElement();   
      14.     Element foo;   
      15.    for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) {   
      16.      foo = (Element) i.next();   
      17.      System.out.print("車牌號(hào)碼:" + foo.elementText("NO"));   
      18.      System.out.println("車主地址:" + foo.elementText("ADDR"));   
      19.     }   
      20.    } catch (Exception e) {   
      21.     e.printStackTrace();   
      22.    }   
      23. }   
      24. }  

      第三種 JDOM實(shí)現(xiàn)方法:

      Java代碼 復(fù)制代碼
      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("data_10k.xml"));   
      12.     Element foo = doc.getRootElement();   
      13.     List allChildren = foo.getChildren();   
      14.    for (int i = 0; i < allChildren.size(); i++) {   
      15.      System.out.print("車牌號(hào)碼:"+ ((Element) allChildren.get(i)).getChild("NO").getText());   
      16.      System.out.println("車主地址:"+ ((Element) allChildren.get(i)).getChild("ADDR").getText());   
      17.     }   
      18.    } catch (Exception e) {   
      19.     e.printStackTrace();   
      20.    }   
      21. }   
      22. }  

      第四種SAX實(shí)現(xiàn)方法:

      Java代碼 復(fù)制代碼
      1. import javax.xml.parsers.SAXParser;   
      2. import javax.xml.parsers.SAXParserFactory;   
      3.   
      4. import org.xml.sax.Attributes;   
      5. import org.xml.sax.InputSource;   
      6. import org.xml.sax.SAXException;   
      7. import org.xml.sax.helpers.DefaultHandler;   
      8.   
      9. public class MyXMLReader2SAX extends DefaultHandler {   
      10.   
      11. java.util.Stack tags = new java.util.Stack();   
      12.   
      13. public MyXMLReader2SAX() {   
      14.   super();   
      15. }   
      16.   
      17. public static void main(String args[]) {   
      18.   long lasting = System.currentTimeMillis();   
      19.   try {   
      20.     SAXParserFactory sf = SAXParserFactory.newInstance();   
      21.     SAXParser sp = sf.newSAXParser();   
      22.     MyXMLReader2SAX reader = new MyXMLReader2SAX();   
      23.     sp.parse(new InputSource("data_10k.xml"), reader);   
      24.    } catch (Exception e) {   
      25.     e.printStackTrace();   
      26.    }   
      27.   
      28.    System.out.println("運(yùn)行時(shí)間:" + (System.currentTimeMillis() - lasting)   
      29.      + "毫秒");   
      30. }   
      31.   
      32. public void characters(char ch[], int start, int length)   
      33.    throws SAXException {   
      34.    String tag = (String) tags.peek();   
      35.   if (tag.equals("NO")) {   
      36.     System.out.print("車牌號(hào)碼:" + new String(ch, start, length));   
      37.    }   
      38.   if (tag.equals("ADDR")) {   
      39.     System.out.println("地址:" + new String(ch, start, length));   
      40.    }   
      41. }   
      42.   
      43. public void startElement(String uri, String localName, String qName,   
      44.     Attributes attrs) {   
      45.    tags.push(qName);   
      46. }   
      47. }  

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多