A simple example of Stax parsing xml

Source: Internet
Author: User

Keyword Explanation:

stax:streaming API FOR XML (stream interface for reading and writing XML)


XML File Code:

<?xml version= "1.0"?>  <service>      <item>          <task>test</task>          < strategy>repeat</strategy>      </item>      <item>          <task>webpageresptask</ task>          <strategy>repeat</strategy>      </item>  </service>  


Java code: JUNIT4 testcase:teststax

Package Unit_test;import Java.io.inputstream;import Javax.xml.stream.xmleventreader;import Javax.xml.stream.xmlinputfactory;import Javax.xml.stream.xmlstreamconstants;import Javax.xml.stream.xmlstreamexception;import Javax.xml.stream.xmlstreamreader;import Javax.xml.stream.events.xmlevent;import Org.junit.test;public class Teststax {//cursor-based parsing method @testpublic void         Cursorbased () {Xmlinputfactory factory = Xmlinputfactory.newinstance ();         InputStream is = null;         is = TestStax.class.getClassLoader (). getResourceAsStream ("Service.xml");         Xmlstreamreader reader = null; try {reader = Factory.createxmlstreamreader (IS), while (Reader.hasnext ()) {int type = Reader.next ();//System.out . println (Reader.next ());//if (type==xmlstreamconstants.start_document| | Type==xmlstreamconstants.end_document) {//SYSTEM.OUT.PRINTLN (reader: ToString ());/} if (Type==xmlstreamconstants.start_element| | Type==xmlstreamconstants.end_element) {System.out.println (reader.gEtname (). toString ());}          else if (type==xmlstreamconstants.characters) {System.out.println (Reader.gettext (). toString (). Trim ());}}}            catch (Xmlstreamexception e) {e.printstacktrace ();}         }//based on iterative parsing method @testpublic void iteratorbased () {Xmlinputfactory factory = Xmlinputfactory.newinstance ();         InputStream is = null;         is = TestStax.class.getClassLoader (). getResourceAsStream ("Service.xml");         Xmleventreader reader = null;    try {reader = Factory.createxmleventreader (IS), while (Reader.hasnext ()) {Xmlevent event = reader.nextevent (); if (Event.isstartelement ()) {String name = Event.asstartelement (). GetName (). toString (); if (Name.equals ("strategy")) { System.out.println (Reader.getelementtext ());}}      }} catch (Xmlstreamexception e) {e.printstacktrace ();}  }}



A simple example of Stax 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.