Four ways to parse XML in Java

Source: Internet
Author: User

XML has now become a universal Data Interchange format, its platform-independent, language-independent, system-independent, to the data integration and interaction brought great convenience. The syntax and technical details of the XML itself need to be read in the relevant technical literature, which includes the DOM (document Object Model), the DTD (document Type Definition), SAX (Simple API for XML), XSD (XML Schema Definition), XSLT (extensible Stylesheet Language Transformations), can be found in the official website document HTTP// www.w3.org for more information.

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








</employees>

This article uses the Java language to generate and parse XML documents for DOM and sax.
First, define an interface for manipulating XML documents XmlDocument it defines the interface for establishing and parsing XML documents.


















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 structure that resides in memory, and the code can then manipulate the tree structure using the DOM interface. Advantages: The entire document tree in memory, easy to operate, support delete, modify, rearrange and other functions; disadvantage: The entire document into memory (including useless nodes), waste time and space; Use cases: Once the document is parsed, the data must be accessed multiple times, and the hardware resources are sufficient (memory, CPU).

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 of 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 (This.document.createTextNode ("Ding-sonorous"));
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 successfully!");
} 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 completed");
} 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 has emerged to solve the DOM problem. SAX, event-driven. When the parser discovers the start of an element, the end of an element, text, the beginning or end of a document, and so on, the programmer writes the code that responds to these events and saves the data. Pros: No need to transfer the entire document in advance, consume less resources, SAX parser code is smaller than DOM parser code, suitable for applets, download. Cons: not persistent; 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 the text is not known which element; use case: Applet; only a small amount of XML documents, little back access, less machine memory;

Java code

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 ("The document begins to print");

}

public void Enddocument () throws Saxexception {

System.out.println ("Document printing finished");

}

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 ("The document begins to print");
}
public void Enddocument () throws Saxexception {
System.out.println ("Document printing finished");
}
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 Generating and parsing XML documents

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

Java code

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

In order to reduce the number of DOM, sax encoding, there is a jdom; Pros: 20-80 principle, greatly reducing the amount of code. Use occasions: to achieve simple functions, such as parsing, creation, etc., but at the bottom, Jdom still use sax (most commonly used), DOM, Xanan documents.





Four ways to parse XML in Java

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.