StAX (streaming API for XML) is a stream-oriented pull parsing XML that is fast, resource-intensive, and very suitable for XML files that handle large amounts of data.
Detailed tutorials and instructions can be found in the following articles:
Parsing XML using StAX, part 1th: Introduction to streaming API for XML (StAX)
Http://www.ibm.com/developerworks/cn/xml/x-stax1.html
Parsing XML using StAX, part 2nd: Pull parsing and events
Http://www.ibm.com/developerworks/cn/xml/x-stax2.html
Parsing XML with StAX, part 3rd: Using custom events and writing XML
Http://www.ibm.com/developerworks/cn/xml/x-stax3.html
The stax--of Java6.0 new features comprehensive analysis of Java XML parsing technology
http://zangweiren.iteye.com/blog/647334
Geronimo Traitor: Using the integration package: Codehaus's Woodstox
http://www.ibm.com/developerworks/cn/opensource/os-ag-renegade15/
The purpose of this article is to illustrate the StAX2 application in the Woodstox package.
Woodstox official website http://woodstox.codehaus.org/
Download Woodstox-core.jar, the core package has two open source protocols Apache ASL and popular LGPL, while Woodstox-core.jar need Stax2-api.jar support
Stax2 and Stax are somewhat different and incompatible with the original Stax code
Read operation:
Java code
- Public XMLStreamReader2 Getstreamreader (String xmlstr) throws xmlstreamexception {
- XMLInputFactory2 Xmlif = (XMLInputFactory2) XMLInputFactory2
- . newinstance ();
- Xmlif.setproperty (Xmlinputfactory.is_replacing_entity_references,
- Boolean.false);
- Xmlif.setproperty (Xmlinputfactory.is_supporting_external_entities,
- Boolean.false);
- Xmlif.setproperty (xmlinputfactory.is_coalescing, Boolean.false);
- Xmlif.configureforspeed ();
- XMLStreamReader2 XMLR = (XMLStreamReader2) xmlif.createxmlstreamreader (new BufferedReader ( (XMLSTR)));
- return XMLR;
- }
- Public XMLStreamReader2 Getstreamreader (InputStream is) throws Xmlstreamexception, IOException {
- XMLInputFactory2 Xmlif = (XMLInputFactory2) XMLInputFactory2
- . newinstance ();
- Xmlif.setproperty (Xmlinputfactory.is_replacing_entity_references,
- Boolean.false);
- Xmlif.setproperty (Xmlinputfactory.is_supporting_external_entities,
- Boolean.false);
- Xmlif.setproperty (xmlinputfactory.is_coalescing, Boolean.false);
- Xmlif.configureforspeed ();
- XMLStreamReader2 XMLR = (XMLStreamReader2) xmlif.createxmlstreamreader (new BufferedReader (new InputStreamReader (IS, "UTF-8")));
- return XMLR;
- }
- XMLStreamReader2 XMLSR = null;
- try {
- XMLSR = this.getstreamreader (str);
- int eventtype = Xmlsr.geteventtype ();
- List = new arraylist<ofcardmainclass> ();
- //Packaging Large-class data
- Ofcardmainclass classof = null;
- While (Xmlsr.hasnext ()) {
- EventType = Xmlsr.next ();
- switch (eventtype) {
- Case Xmlevent2.start_element:
- String name = Xmlsr.getname (). Getlocalpart ();
- if (name.equals ("AA"))
- String S1 = Xmlsr.getelementtext ();
- if (name.equals ("BB"))
- String s2 = xmlsr.getattributevalue (null, "ATT"));
- Break ;
- Case Xmlevent2.end_element:
- if (Xmlsr.getname (). Getlocalpart (). Equals (
- "AA"))
- Break ;
- }
- }
- } finally {
- if (XMLSR! = null)
- Xmlsr.close ();
- }
Woodstox-based Stax 2 (streaming API for XML) parsing XML