Four Methods for parsing XML in Java

Source: Internet
Author: User

XML has now become a common data exchange format. Its platform independence, language independence, and system independence bring great convenience to data integration and interaction. For the syntax knowledge and technical details of XML, you need to read the relevant technical documents, including the content of DOM (Document Object Model), DTD (Document Type Definition ), SAX (Simple API for XML), XSD (Xml Schema Definition), XSLT (Extensible Stylesheet Language Transformations ),
XML is parsed in the same way in different languages, but the syntax is different. There are two basic parsing Methods: SAX and DOM. SAX is based on event stream parsing, and DOM is based on XML document tree structure parsing. Assume that the content and structure of XML are 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 generate and parse XML documents of DOM and SAX.
First, define an XML document operation interface XmlDocument which defines the interface for creating and parsing XML documents.

Package com. alisoft. facepay. framework. bean;
/**
*
* @ Author hongliang. dinghl
* Defines interfaces for creating and parsing XML documents
*/
Public interface XmlDocument {
/**
* Create an XML document
* @ Param fileName: full path name of the file
*/
Public void createXml (String fileName );
/**
* Parse XML documents
* @ Param fileName: full path name of the file
*/
Public void parserXml (String fileName );
}
1. DOM generation and parsing of XML documents

Defines a set of interfaces for the parsed versions of the XML document. The parser reads the entire document, constructs a memory-resident tree structure, and then the code can use the DOM interface to operate on this tree structure. Advantage: the entire document tree is in the memory for easy operation. It supports multiple features such as deletion, modification, and rescheduling. disadvantage: transferring the entire document to the memory (including useless nodes ), A waste of time and space. Use Cases: Once the documents are parsed, the data needs to be accessed multiple times. There are sufficient hardware resources (memory and 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.doc ument = builder. newDocument ();
} Catch (ParserConfigurationException e ){
System. out. println (e. getMessage ());
}
}
Public void createXml (String fileName ){
Element root = this.doc ument. createElement ("employees ");
This.doc ument. appendChild (root );
Element employee = this.doc ument. createElement ("employee ");
Element name = this.doc ument. createElement ("name ");
Name.appendChild(this.doc ument. createTextNode ("ding hongliang "));
Employee. appendChild (name );
Element sex = this.doc ument. createElement ("sex ");
Sex.appendChild(this.doc ument. createTextNode ("m "));
Employee. appendChild (sex );
Element age = this.doc ument. createElement ("age ");
Age.appendChild(this.doc ument. 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 ("XML file generated 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 ("parsed ");
} 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. Generate and parse XML documents using SAX

To solve DOM problems, the following occurs. Event-driven. When the parser finds the start, end, text, start, or end of an element, it sends events and programmers write code to respond to these events and save data. Advantage: you do not need to call the entire document in advance, which consumes less resources. The code of the SAX Parser is smaller than that of the DOM parser. It is suitable for Applet and download. Disadvantages: it is not persistent. After an event, if no data is saved, the data is lost; stateless; only the text can be obtained from the event, but the element of the text is unknown; application Scenario: Applet; only a small amount of content in the XML document is required, which is rarely accessed back; fewer 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 is printed ");

}

Public void endDocument () throws SAXException {

System. out. println ("document printing is 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 is printed ");
}
Public void endDocument () throws SAXException {
System. out. println ("document printing is 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 generate and parse XML documents

DOM4J is a very good Java xml api with excellent performance, powerful functionality and extreme ease of use. It is also an open source software. Now you can see that more and more Java software are using DOM4J to read and write XML. It is particularly worth mentioning that Sun's JAXM is also 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. extends entexception;
Import org. dom4j. DocumentHelper;
Import org. dom4j. Element;
Import org. dom4j. io. SAXReader;
Import org. dom4j. io. XMLWriter;
/**
*
* @ Author hongliang. dinghl
* Dom4j generates XML documents and parses XML documents
*/
Public class Dom4jDemo implements XmlDocument {

Public void createXml (String fileName ){
Document document = incluenthelper. 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 (incluentexception e ){
System. out. println (e. getMessage ());
}
System. out. println ("dom4j parserXml ");
}
}
4. JDOM generation and parsing XML

To reduce the DOM and SAX encoding volumes, JDOM emerged. Advantage: The 20-80 principle greatly reduces the amount of code. Usage: The functions to be implemented are simple, such as parsing and creation. However, at the underlying layer, JDOM still uses the SAX (most commonly used), DOM, and Xanan documents.


[Java]
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 generation and parsing of 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; I <EMPLOYEELIST. SIZE (); I ++ ){
Element employee = (Element) employeeList. get (I );
List employeeInfo = employee. getChildren ();
For (int j = 0; j <EMPLOYEEINFO. SIZE (); J ++ ){
System. out. println (Element) employeeInfo. get (j )). getName () + ":" + (Element) employeeInfo. get (j )). getValue ());
 
}
}
} Catch (JDOMException e ){
 
E. printStackTrace ();
} Catch (IOException e ){
 
E. printStackTrace ();
}
 
}
}
<P> </p>

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.