Four ways to read XML files----java

Source: Internet
Author: User
Tags gettext

XML file:

XML code

    <?XML version= "1.0" encoding= "GB2312"?>      <RESULT>     <VALUE>     <NO>A1234</NO>     <ADDR>Zhengzhou City, Henan Province</ADDR>     </VALUE>     <VALUE>     <NO>B1234</NO>     <ADDR>Erqi District, Zhengzhou city, Henan Province</ADDR>     </VALUE>     </RESULT>

The first method of DOM implementation:

Java code

1       ImportJava.io.File;2     ImportJavax.xml.parsers.DocumentBuilder;3     Importjavax.xml.parsers.DocumentBuilderFactory;4     Importorg.w3c.dom.Document;5     Importorg.w3c.dom.NodeList;6      Public classMyxmlreader2dom {7      Public Static voidMain (String arge[]) {8     LongLasting =System.currenttimemillis ();9     Try { TenFile f =NewFile ("Data_10k.xml");  OneDocumentbuilderfactory factory =documentbuilderfactory.newinstance (); ADocumentbuilder Builder =Factory.newdocumentbuilder (); -Document doc =Builder.parse (f); -NodeList nl = doc.getelementsbytagname ("VALUE");  the      for(inti = 0; I < nl.getlength (); i++) {  -System.out.print ("License plate number:" + doc.getelementsbytagname ("NO")). Item (i). Getfirstchild (). Getnodevalue ());  -System.out.println ("Owner's address:" + doc.getelementsbytagname ("ADDR")). Item (i). Getfirstchild (). Getnodevalue ());  -System.out.println ("Run Time:" + (System.currenttimemillis ()-lasting) ++ "milliseconds");  - } + } A}Catch(Exception e) { at e.printstacktrace (); - } - } -}

The second, DOM4J implementation method:

Java code


1 ImportJava.io.*; 2     ImportJava.util.*; 3     Importorg.dom4j.*; 4     ImportOrg.dom4j.io.*; 5      Public classmyxmlreader2dom4j {6      Public Static voidMain (String arge[]) {7     LongLasting =System.currenttimemillis ();8     Try { 9File f =NewFile ("Data_10k.xml"); TenSaxreader reader =NewSaxreader (); OneDocument doc =Reader.read (f); AElement root =doc.getrootelement (); - Element foo; -      for(Iterator i = root.elementiterator ("VALUE")); I.hasnext ();) {  theFoo =(Element) I.next (); -System.out.print ("License plate number:" + foo.elementtext ("NO")));  -System.out.println ("Owner's address:" + foo.elementtext ("ADDR")));  - } +System.out.println ("Run Time:" + (System.currenttimemillis ()-lasting) -+ "milliseconds");  + } A}Catch(Exception e) { at e.printstacktrace (); - } - } -}

The third method of jdom implementation:

Java code

1       ImportJava.io.*; 2     ImportJava.util.*; 3     Importorg.jdom.*; 4     Importorg.jdom.input.*; 5      Public classMyxmlreader2jdom {6      Public Static voidMain (String arge[]) {7     LongLasting =System.currenttimemillis ();8     Try { 9Saxbuilder Builder =NewSaxbuilder ();TenDocument doc = Builder.build (NewFile ("Data_10k.xml"));  OneElement foo =doc.getrootelement (); AList Allchildren =Foo.getchildren (); -      for(inti = 0; I < allchildren.size (); i++) {  -System.out.print ("License plate Number:" + (Element) allchildren.get (i)). Getchild ("NO"). GetText ());  theSystem.out.println ("Owner's Address:" + (Element) allchildren.get (i)). Getchild ("ADDR"). GetText ());  - } -System.out.println ("Run Time:" + (System.currenttimemillis ()-lasting) -+ "milliseconds");  + } -}Catch(Exception e) { + e.printstacktrace (); A } at } -}

The fourth method of sax implementation:

Java code

 

1    ImportJavax.xml.parsers.SAXParser;2     Importjavax.xml.parsers.SAXParserFactory;3     Importorg.xml.sax.Attributes;4     ImportOrg.xml.sax.InputSource;5     Importorg.xml.sax.SAXException;6     ImportOrg.xml.sax.helpers.DefaultHandler;7      Public classMyxmlreader2saxextendsDefaultHandler {8Java.util.Stack tags =NewJava.util.Stack ();9      PublicMyxmlreader2sax () {Ten     Super();  One } A      Public Static voidMain (String args[]) { -     LongLasting =System.currenttimemillis (); -     Try {  theSAXParserFactory SF =saxparserfactory.newinstance (); -SAXParser SP =Sf.newsaxparser (); -Myxmlreader2sax reader =NewMyxmlreader2sax (); -Sp.parse (NewInputSource ("Data_10k.xml"), reader);  +}Catch(Exception e) { - e.printstacktrace (); + } ASystem.out.println ("Run Time:" + (System.currenttimemillis ()-lasting) at+ "milliseconds");  - } -      Public voidCharacters (CharCh[],intStartintlength) -     throwssaxexception { -String tag =(String) Tags.peek (); -     if(Tag.equals ("NO")) {  inSystem.out.print ("License plate number:" +NewString (CH, start, length));  - } to     if(Tag.equals ("ADDR")) {  +SYSTEM.OUT.PRINTLN ("Address:" +NewString (CH, start, length));  - } the } *      Public voidstartelement (String uri, String localname, String qName, $ Attributes attrs) { Panax Notoginseng Tags.push (qName); - } the}

Four ways to read XML files----Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.