- 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:
-
-
- Objects created from xmlevent cannot be changed. Can be used for array,list,map.
- can inherit xmlevent.
- 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:
-
-
- Use Javax.xml.stream.XMLInputFactory System properties;
- Use the JRE under the Lib/xml.stream.properties file;
- Use the services API to find in meta-inf/services/javax.xml.stream.xmlinputfactory;
- 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