Several XML parsing Methods

Source: Internet
Author: User

XML has now become a common data exchange format. The independence of the platform makes it necessary for many occasions to use XML. This article describes in detail four methods for parsing XML using Java. 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 ), for more information, see the W3C official website documentation http://www.w3.org.

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;
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 ());
}
}
}

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.