Four ways to parse XML in Java _java

Source: Internet
Author: User
Tags getmessage object model

XML has now become a general Data Interchange format, its platform independent, language-independent, system-independent, which brings great convenience to data integration and interaction. For the syntax knowledge and technical details of the XML itself, it is necessary to read the relevant technical literature, which includes the DOM (document Object Model), the DTD (document Type Definition), and SAX (simple APIs for XML), XSD (XML Schema Definition), XSLT (extensible Stylesheet Language Transformations), refer to the official web site documentation http:// www.w3.org get more information.

XML is interpreted in the same way in different languages, except that the syntax of the implementation is different. There are two basic parsing methods, one called Sax and the other called Dom. Sax is based on parsing of event streams, and Dom is based on XML document tree structure parsing. Suppose the content and structure of our XML are as follows:

Copy Code code as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<employees>
<employee>
<name>ddviplinux</name>
<sex>m</sex>
<age>30</age>
</employee>
</employees>


This article uses the Java language to implement the XML document generation and parsing of Dom and sax.
First, define an interface for manipulating XML documents XmlDocument it defines an interface for establishing and parsing XML documents.

Copy Code code as follows:

Package Com.alisoft.facepay.framework.bean;
/**
*
* @author HONGLIANG.DINGHL
* Defines an interface for XML document creation and resolution
*/
Public interface XmlDocument {
/**
* Create an XML document
* @param filename File full path name
*/
public void Createxml (String fileName);
/**
* Parse XML document
* @param filename File full path name
*/
public void Parserxml (String fileName);
}


1.DOM Generating and parsing XML documents

Defines a set of interfaces for the parsed version of an XML document. The parser reads the entire document and then constructs a tree that resides in memory, and then the code can manipulate the tree structure using the DOM interface. Advantage: The entire document tree is in memory, easy to operate, support delete, modify, rearrange and so on a variety of functions; disadvantages: The entire document into memory (including useless nodes), waste of time and space; usage: Once the document is parsed, the data needs to be accessed more than once; hardware resources are sufficient (memory, CPU).
Copy Code code as follows:

Package Com.alisoft.facepay.framework.bean;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.PrintWriter;
Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import javax.xml.parsers.ParserConfigurationException;
Import Javax.xml.transform.OutputKeys;
Import Javax.xml.transform.Transformer;
Import javax.xml.transform.TransformerConfigurationException;
Import javax.xml.transform.TransformerException;
Import Javax.xml.transform.TransformerFactory;
Import Javax.xml.transform.dom.DOMSource;
Import Javax.xml.transform.stream.StreamResult;
Import org.w3c.dom.Document;
Import org.w3c.dom.Element;
Import Org.w3c.dom.Node;
Import org.w3c.dom.NodeList;
Import org.xml.sax.SAXException;
/**
*
* @author HONGLIANG.DINGHL
* Dom Generation and parsing XML documents
*/
Public class Domdemo implements XmlDocument {
Private document document;
Private String FileName;
public void init () {
try {
Documentbuilderfactory factory = Documentbuilderfactory
. newinstance ();
Documentbuilder builder = Factory.newdocumentbuilder ();
This.document = Builder.newdocument ();
catch (Parserconfigurationexception e) {
System.out.println (E.getmessage ());
}
}
public void Createxml (String fileName) {
Element root = this.document.createElement ("Employees");
This.document.appendChild (root);
Element employee = this.document.createElement ("employee");
Element name = this.document.createElement ("name");
Name.appendchild ("ding-this.document.createTextNode");
Employee.appendchild (name);
Element sex = this.document.createElement ("Sex");
Sex.appendchild (This.document.createTextNode ("M"));
Employee.appendchild (Sex);
Element age = this.document.createElement (' age ');
Age.appendchild (This.document.createTextNode ("30"));
Employee.appendchild (age);
Root.appendchild (employee);
Transformerfactory tf = Transformerfactory.newinstance ();
try {
Transformer Transformer = Tf.newtransformer ();
Domsource Source = new Domsource (document);
Transformer.setoutputproperty (outputkeys.encoding, "gb2312");
Transformer.setoutputproperty (outputkeys.indent, "yes");
PrintWriter pw = new PrintWriter (new FileOutputStream (FileName));
Streamresult result = new Streamresult (PW);
Transformer.transform (source, result);
SYSTEM.OUT.PRINTLN ("Generate XML File Success!");
catch (Transformerconfigurationexception e) {
System.out.println (E.getmessage ());
catch (IllegalArgumentException e) {
System.out.println (E.getmessage ());
catch (FileNotFoundException e) {
System.out.println (E.getmessage ());
catch (Transformerexception e) {
System.out.println (E.getmessage ());
}
}
public void Parserxml (String fileName) {
try {
Documentbuilderfactory dbf = Documentbuilderfactory.newinstance ();
Documentbuilder db = Dbf.newdocumentbuilder ();
Document document = Db.parse (FileName);
NodeList employees = Document.getchildnodes ();
for (int i = 0; i < employees.getlength (); i++) {
Node employee = Employees.item (i);
NodeList EmployeeInfo = Employee.getchildnodes ();
for (int j = 0; J < Employeeinfo.getlength (); j + +) {
Node node = Employeeinfo.item (j);
NodeList Employeemeta = Node.getchildnodes ();
for (int k = 0; k < employeemeta.getlength (); k++) {
System.out.println (Employeemeta.item (k). Getnodename ()
+ ":" + Employeemeta.item (k). Gettextcontent ());
}
}
}
System.out.println ("Parse complete");
catch (FileNotFoundException e) {
System.out.println (E.getmessage ());
catch (Parserconfigurationexception e) {
System.out.println (E.getmessage ());
catch (Saxexception e) {
System.out.println (E.getmessage ());
catch (IOException e) {
System.out.println (E.getmessage ());
}
}
}


2.SAX Generating and parsing XML documents

Sax appears to solve the DOM problem. SAX, event-driven. When the parser discovers the start of the element, the end of the element, the text, the beginning or the end of the document, send the event, the programmer writes the code that responds to the events, and saves the data. Advantages: Without the prior transfer of the entire document, less resources; The SAX parser code is smaller than the DOM parser code, suitable for applets, downloads. Disadvantage: not lasting; After the event, if the data is not saved, then the data is lost; stateless; only the text can be obtained from the event, but it is not known which element the text belongs to; usage: Applet; only a small amount of content in an XML document;
Java code

Copy Code code as follows:

Package Com.alisoft.facepay.framework.bean;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.InputStream;
Import javax.xml.parsers.ParserConfigurationException;
Import Javax.xml.parsers.SAXParser;
Import Javax.xml.parsers.SAXParserFactory;
Import org.xml.sax.Attributes;
Import org.xml.sax.SAXException;
Import Org.xml.sax.helpers.DefaultHandler;
/**
*
* @author HONGLIANG.DINGHL
* Sax Document parsing
*/
Public class Saxdemo implements XmlDocument {
public void Createxml (String fileName) {
System.out.println ("<<" +filename+ ">>");
}
public void Parserxml (String fileName) {
SAXParserFactory SAXFAC = Saxparserfactory.newinstance ();
try {
SAXParser saxparser = Saxfac.newsaxparser ();
InputStream is = new FileInputStream (fileName);
Saxparser.parse (IS, new Mysaxhandler ());
catch (Parserconfigurationexception e) {
E.printstacktrace ();
catch (Saxexception e) {
E.printstacktrace ();
catch (FileNotFoundException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
}
}
Class Mysaxhandler extends DefaultHandler {
Boolean hasattribute = false;
Attributes Attributes = null;
public void Startdocument () throws Saxexception {
System.out.println ("document started printing");
}
public void Enddocument () throws Saxexception {
System.out.println ("End of document Printing");
}
public void Startelement (string uri, String localname, String qName,
Attributes Attributes) throws Saxexception {
if (Qname.equals ("Employees")) {
Return
}
if (Qname.equals ("employee")) {
System.out.println (QName);
}
if (attributes.getlength () > 0) {
this.attributes = attributes;
This.hasattribute = true;
}
}
public void EndElement (string uri, String localname, String qName)
Throws Saxexception {
if (Hasattribute && (attributes!= null)) {
for (int i = 0; i < attributes.getlength (); i++) {
System.out.println (attributes.getqname (0)
+ attributes.getvalue (0));
}
}
}
public void characters (char[] ch, int start, int length)
Throws Saxexception {
System.out.println (New String (CH, start, length));
}
}
Package Com.alisoft.facepay.framework.bean;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.InputStream;
Import javax.xml.parsers.ParserConfigurationException;
Import Javax.xml.parsers.SAXParser;
Import Javax.xml.parsers.SAXParserFactory;
Import org.xml.sax.Attributes;
Import org.xml.sax.SAXException;
Import Org.xml.sax.helpers.DefaultHandler;
/**
*
* @author HONGLIANG.DINGHL
* Sax Document parsing
*/
Public class Saxdemo implements XmlDocument {
public void Createxml (String fileName) {
System.out.println ("<<" +filename+ ">>");
}
public void Parserxml (String fileName) {
SAXParserFactory SAXFAC = Saxparserfactory.newinstance ();
try {
SAXParser saxparser = Saxfac.newsaxparser ();
InputStream is = new FileInputStream (fileName);
Saxparser.parse (IS, new Mysaxhandler ());
catch (Parserconfigurationexception e) {
E.printstacktrace ();
catch (Saxexception e) {
E.printstacktrace ();
catch (FileNotFoundException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
}
}
Class Mysaxhandler extends DefaultHandler {
Boolean hasattribute = false;
Attributes Attributes = null;
public void Startdocument () throws Saxexception {
System.out.println ("document started printing");
}
public void Enddocument () throws Saxexception {
System.out.println ("End of document Printing");
}
public void Startelement (string uri, String localname, String qName,
Attributes Attributes) throws Saxexception {
if (Qname.equals ("Employees")) {
Return
}
if (Qname.equals ("employee")) {
System.out.println (QName);
}
if (attributes.getlength () > 0) {
this.attributes = attributes;
This.hasattribute = true;
}
}
public void EndElement (string uri, String localname, String qName)
Throws Saxexception {
if (Hasattribute && (attributes!= null)) {
for (int i = 0; i < attributes.getlength (); i++) {
System.out.println (attributes.getqname (0)
+ attributes.getvalue (0));
}
}
}
public void characters (char[] ch, int start, int length)
Throws Saxexception {
System.out.println (New String (CH, start, length));
}
}


3.dom4j generate and parse XML documents

Dom4j is a very, very good Java XML API with excellent performance, powerful features and extreme ease of use, and it is also an open-source software. Now you can see that more and more Java software is using dom4j to read and write XML, and it is particularly worth mentioning that even Sun's JAXM is using DOM4J.

Java code
Copy Code code as follows:

Package Com.alisoft.facepay.framework.bean;
Import Java.io.File;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.Writer;
Import Java.util.Iterator;
Import org.dom4j.Document;
Import org.dom4j.DocumentException;
Import Org.dom4j.DocumentHelper;
Import org.dom4j.Element;
Import Org.dom4j.io.SAXReader;
Import Org.dom4j.io.XMLWriter;
/**
*
* @author HONGLIANG.DINGHL
* DOM4J generate XML documents and parse XML documents
*/
Public class Dom4jdemo implements XmlDocument {
public void Createxml (String fileName) {
Document document = Documenthelper.createdocument ();
Element employees=document.addelement ("Employees");
Element employee=employees.addelement ("employee");
Element name= employee.addelement ("name");
Name.settext ("Ddvip");
Element sex=employee.addelement ("sex");
Sex.settext ("M");
Element age=employee.addelement ("Age");
Age.settext ("29");
try {
Writer filewriter=new FileWriter (fileName);
XMLWriter xmlwriter=new XMLWriter (fileWriter);
Xmlwriter.write (document);
Xmlwriter.close ();
catch (IOException e) {
System.out.println (E.getmessage ());
}
}
public void Parserxml (String fileName) {
File Inputxml=new file (fileName);
Saxreader Saxreader = new Saxreader ();
try {
Document document = Saxreader.read (Inputxml);
Element employees=document.getrootelement ();
for (Iterator i = Employees.elementiterator (); I.hasnext ();) {
Element employee = (Element) I.next ();
for (Iterator J = employee.elementiterator (); J.hasnext ();) {
Element node= (Element) J.next ();
System.out.println (Node.getname () + ":" +node.gettext ());
}
}
catch (Documentexception e) {
System.out.println (E.getmessage ());
}
System.out.println ("dom4j parserxml");
}
}


4.JDOM Generating and parsing xml

To reduce the number of DOM, Sax coding, there are jdom, advantages: 20-80 principles, greatly reducing the amount of code. Usage: Simple to implement, such as parsing, creating, but at the bottom, Jdom still uses sax (most commonly used), DOM, Xanan documents.
Copy Code code as follows:

Package Com.alisoft.facepay.framework.bean;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import java.util.List;
Import org.jdom.Document;
Import org.jdom.Element;
Import org.jdom.JDOMException;
Import Org.jdom.input.SAXBuilder;
Import Org.jdom.output.XMLOutputter;
/**
*
* @author HONGLIANG.DINGHL
* JDOM generate and parse XML documents
*
*/
Public class Jdomdemo implements XmlDocument {
public void Createxml (String fileName) {
Document document;
Element Root;
Root=new Element ("Employees");
Document=new Document (root);
Element employee=new Element ("employee");
Root.addcontent (employee);
Element name=new Element ("name");
Name.settext ("Ddvip");
Employee.addcontent (name);
Element sex=new element ("sex");
Sex.settext ("M");
Employee.addcontent (Sex);
Element age=new Element ("age");
Age.settext ("23");
Employee.addcontent (age);
Xmloutputter xmlout = new Xmloutputter ();
try {
Xmlout.output (document, New FileOutputStream (FileName));
catch (FileNotFoundException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
}
public void Parserxml (String fileName) {
Saxbuilder builder=new Saxbuilder (false);
try {
Document Document=builder.build (fileName);
Element employees=document.getrootelement ();
List Employeelist=employees.getchildren ("employee");
for (int i=0;ielement employee= (Element) employeelist.get (i);
List Employeeinfo=employee.getchildren ();
for (int j=0;jsystem.out.println ((Element) Employeeinfo.get (j)). GetName () + ":" + ((Element) Employeeinfo.get (j)). GetValue ());
}
}
catch (Jdomexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
}
}

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.