Four ways to read XML files in Java

Source: Internet
Author: User
Tags gettext

XML file:

XML code


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

The first method of DOM implementation:

Java code


Import Java.io.File;
Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import org.w3c.dom.Document;
Import org.w3c.dom.NodeList;
public class Myxmlreader2dom {
public static void Main (String arge[]) {
Long lasting = System.currenttimemillis ();
try {
File F = new file ("Data_10k.xml");
Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
Documentbuilder builder = Factory.newdocumentbuilder ();
Document doc = Builder.parse (f);
NodeList nl = doc.getelementsbytagname ("VALUE");
for (int i = 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");
}
}
} catch (Exception e) {
E.printstacktrace ();
}
}
}

The second, DOM4J implementation method:

Java code


Import java.io.*;
Import java.util.*;
Import org.dom4j.*;
Import org.dom4j.io.*;
public class Myxmlreader2dom4j {
public static void Main (String arge[]) {
Long lasting = System.currenttimemillis ();
try {
File F = new file ("Data_10k.xml");
Saxreader reader = new Saxreader ();
Document doc = Reader.read (f);
Element root = Doc.getrootelement ();
Element foo;
for (Iterator i = root.elementiterator ("VALUE"); I.hasnext ();) {
Foo = (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");
}
} catch (Exception e) {
E.printstacktrace ();
}
}
}

The third method of jdom implementation:

Java code


Import java.io.*;
Import java.util.*;
Import org.jdom.*;
Import org.jdom.input.*;
public class Myxmlreader2jdom {
public static void Main (String arge[]) {
Long lasting = System.currenttimemillis ();
try {
Saxbuilder builder = new Saxbuilder ();
Document doc = builder.build (new File ("Data_10k.xml"));
Element foo = doc.getrootelement ();
List Allchildren = Foo.getchildren ();
for (int i = 0; i < allchildren.size (); i++) {
System.out.print ("License plate Number:" + (Element) allchildren.get (i)). Getchild ("NO"). GetText ());
System.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 ();
}
}
}

The fourth method of sax implementation:

Java code

 


Import Javax.xml.parsers.SAXParser;
Import Javax.xml.parsers.SAXParserFactory;
Import org.xml.sax.Attributes;
Import Org.xml.sax.InputSource;
Import org.xml.sax.SAXException;
Import Org.xml.sax.helpers.DefaultHandler;
public class Myxmlreader2sax extends DefaultHandler {
Java.util.Stack tags = new java.util.Stack ();
Public Myxmlreader2sax () {
Super ();
}
public static void Main (String args[]) {
Long lasting = System.currenttimemillis ();
try {
SAXParserFactory SF = Saxparserfactory.newinstance ();
SAXParser sp = Sf.newsaxparser ();
Myxmlreader2sax reader = new Myxmlreader2sax ();
Sp.parse (New InputSource ("Data_10k.xml"), reader);
} catch (Exception e) {
E.printstacktrace ();
}
System.out.println ("Run Time:" + (System.currenttimemillis ()-lasting)
+ "milliseconds");
}
public void characters (char ch[], int start, int length)
Throws Saxexception {
String tag = (string) tags.peek ();
if (Tag.equals ("NO")) {
System.out.print ("License plate number:" + New String (CH, start, length));
}
if (Tag.equals ("ADDR")) {
SYSTEM.OUT.PRINTLN ("Address:" + New String (CH, start, length));
}
}
public void Startelement (string uri, String localname, String qName,
Attributes attrs) {
Tags.push (QName);
}
}

Four ways to read XML files in 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.