Woodstox-based Stax 2 (streaming API for XML) parsing XML

Source: Internet
Author: User

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
    1. Public XMLStreamReader2 Getstreamreader (String xmlstr) throws xmlstreamexception {
    2. XMLInputFactory2 Xmlif = (XMLInputFactory2) XMLInputFactory2
    3. . newinstance ();
    4. Xmlif.setproperty (Xmlinputfactory.is_replacing_entity_references,
    5. Boolean.false);
    6. Xmlif.setproperty (Xmlinputfactory.is_supporting_external_entities,
    7. Boolean.false);
    8. Xmlif.setproperty (xmlinputfactory.is_coalescing, Boolean.false);
    9. Xmlif.configureforspeed ();
    10. XMLStreamReader2 XMLR = (XMLStreamReader2) xmlif.createxmlstreamreader (new BufferedReader (  (XMLSTR)));
    11. return XMLR;
    12. }
    13. Public XMLStreamReader2 Getstreamreader (InputStream is) throws Xmlstreamexception, IOException {
    14. XMLInputFactory2 Xmlif = (XMLInputFactory2) XMLInputFactory2
    15. . newinstance ();
    16. Xmlif.setproperty (Xmlinputfactory.is_replacing_entity_references,
    17. Boolean.false);
    18. Xmlif.setproperty (Xmlinputfactory.is_supporting_external_entities,
    19. Boolean.false);
    20. Xmlif.setproperty (xmlinputfactory.is_coalescing, Boolean.false);
    21. Xmlif.configureforspeed ();
    22. XMLStreamReader2 XMLR = (XMLStreamReader2) xmlif.createxmlstreamreader (new BufferedReader (new  InputStreamReader (IS, "UTF-8")));
    23. return XMLR;
    24. }
    25. XMLStreamReader2 XMLSR = null;
    26. try {
    27. XMLSR = this.getstreamreader (str);
    28. int eventtype = Xmlsr.geteventtype ();
    29. List = new arraylist<ofcardmainclass> ();
    30. //Packaging Large-class data
    31. Ofcardmainclass classof = null;
    32. While (Xmlsr.hasnext ()) {
    33. EventType = Xmlsr.next ();
    34. switch (eventtype) {
    35. Case Xmlevent2.start_element:
    36. String name = Xmlsr.getname (). Getlocalpart ();
    37. if (name.equals ("AA"))
    38. String S1 = Xmlsr.getelementtext ();
    39. if (name.equals ("BB"))
    40. String s2 = xmlsr.getattributevalue (null, "ATT"));
    41. Break ;
    42. Case Xmlevent2.end_element:
    43. if (Xmlsr.getname (). Getlocalpart (). Equals (
    44. "AA"))
    45. Break ;
    46. }
    47. }
    48. } finally {
    49. if (XMLSR! = null)
    50. Xmlsr.close ();
    51. }

Woodstox-based Stax 2 (streaming API for XML) parsing 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.