Java Methods for processing XML

Source: Internet
Author: User

    • Dom (Document Object Model) API: As the name implies, read and write XML based on a DOM book.
     Public Static voidMain (string[] args)throwssaxexception, IOException, parserconfigurationexception {documentbuilderfactory dbf=documentbuilderfactory.newinstance (); //Dbf.setnamespaceaware (TRUE); //dbf.setvalidating (true);Documentbuilder db =Dbf.newdocumentbuilder (); OutputStreamWriter Errorwriter=NewOutputStreamWriter (System.err,"UTF-8"); Db.seterrorhandler (NewMyErrorHandler (NewPrintWriter (Errorwriter,true))); Document Doc= Db.parse (NewFile ("Resources/dom_example.xml"));        Doc.getdocumentelement (). normalize (); Element node=doc.getdocumentelement (); Traverse (node,0); }    Private Static voidTraverse (Node node,intDeep ) {        if(NULL==node) {            return; } Node Sibling=node;  while(NULL! = Sibling && Node.element_node = =Sibling.getnodetype ())            {Printnode (node, deep); Traverse (Sibling.getfirstchild (), deep+ 1); Sibling=node.getnextsibling (); }    }

In addition to the JDK's own DOM implementations, the common DOM libraries are dom4j.

    • SAX (Simple API for XML) API: reads XML sequentially. The API can only read XML files. It provides an event-driven approach to parsing XML based on the Push-parsing model.

Since it is push parsing, we need to implement handler (ContentHandler, ErrorHandler, Dtdhandler, etc.) to handle the captured event. Usually we inherit DefaultHandler.

    

SAXParserFactory SPF = saxparserfactory.newinstance ();        Spf.setnamespaceaware (true);         = Spf.newsaxparser ();         = Saxparser.getxmlreader ();        Xmlreader.setcontenthandler (new  saxlocalnamecount ());        Xmlreader.seterrorhandler (new  MyErrorHandler (System.err));        Xmlreader.parse ("Sax_example.xml");

Both DOM and Sax are APIs before 1.6, so it's best not to use them again.

Starting with 1.6, to improve efficiency, several new APIs are available to process xml:

    • Stax (streaming API for XML): An XML processing API based on the pull-parsing model. You can refer to the http://www.xmlpull.org/v1/download/unpacked/doc/quick_intro.html for the contents of the XML pull.

    

The Stax API is divided into the cursor API and the event Iterator API.

The cursor API provides a forward cursor traversal XML. It consists of 2 main interfaces:

        Xmlstreamreader:

        Xmlstreamwriter:

      The event Iterator API regards an XML stream as an object of events. The parser (Parser) Gets the event through pull mode. Mainly includes the following interfaces:

        Xmlevent

Xmleventreader

Xmleventwriter

The Event Iterator API has the following advantages compared to the cursor API:

        1. Objects created from xmlevent cannot be changed. Can be used for array,list,map.
        2. can inherit xmlevent.
        3. More easily add and delete events.

If memory is limited or requires high performance, the cursor API is preferred, and in other cases use the event Iterator API as much as possible.

The xmlinputfactory is used to create an XML stream read. Xmlinputfactory.newinstance follow these steps to determine the Xmlinputfactory instance:

        1. Use Javax.xml.stream.XMLInputFactory System properties;
        2. Use the JRE under the Lib/xml.stream.properties file;
        3. Use the services API to find in meta-inf/services/javax.xml.stream.xmlinputfactory;
        4. Using the system default Xmlinputfactory implementation;

Xmloutputfactory is used to create XML stream writes.

Xmleventfactory is used to create events.

Xmlstreamreader:

              

Xmlinputfactory f = xmlinputfactory.newinstance (); Xmlstreamreader r = f.createxmlstreamreader (...);  while (R.hasnext ()) {    r.next (); }

Xmleventreader:

Xmlstreamwriter:

Xmleventwriter:

 

Report CDATA Event:Javax.xml.stream.XMLStreamReader does not report CDATA events by default. If necessary, you can configure Xmlinputfactory as follows:

Xmlinputfactory factory = xmlinptufactory.newinstance (); Factory.setproperty ("report-cdata-event", Boolean.true);

For specific examples, refer to: http://docs.oracle.com/javase/tutorial/jaxp/stax/example.html

      

A comparison of Stax and other XML APIs can be referred to http://docs.oracle.com/javase/tutorial/jaxp/stax/why.html

    • JAXB (Java Architecture for XML Binding)

Java Methods for processing XML

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.