dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


advertisement
 

KXML: A Great Find for XML Parsing in J2ME  : Page 2

Enhydra's kXML is a great little XML parser with a small footprint, making it perfect for J2ME apps. It uses a unique method of DOM manipulation and parsing called "pull parsing." Find out whether kXML is must-have for your toolbox.


advertisement

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Pull Parsing
Anyway, enough theory. Suffice it to say that kXML is easy to use. So let's look at a quick example that shows how kXML works as a pull parser. The demo is called kXMLDemo_pull. It will use a pull parser to go through a file that contains address book information. Shown below are some of the more important lines in the source code and a description of what they do.

1.XmlParser parser = null; 2. 3.parser = new XmlParser( new InputStreamReader( 1this.getClass().getResourceAsStream(resfile_name) ));

Line 3 creates an XmlParser passing it the InputStream from a resource read in as a stream. This parser is called repeatedly until an END_DOCUMENT event is issued.

1.while ( (event = parser.read()).getType() != Xml.END_DOCUMENT ) { 2. ... 3.if (name != null && name.equals("address")) { 4. ... 5. parseAddressTag( parser );

Line 3 determines if the event is the start of an <address> tag, and line 5 passes the parser to the "parseAddressTag," which takes control of the parser.


1.while ((event = parser.peek()).getType() != Xml.END_DOCUMENT) { 2.... 3. if (type == Xml.END_TAG && name.equals("address")) { 4. return; 5. } 6.... 7. ParseEvent next = parser.read(); 8. 9. // if it's not a text event then skip it 10. if (next.getType() != Xml.TEXT) { 11. continue; 12. } 13.... 14. System.err.println(name + ": " + text);

The code immediately above is inside the "parseAddressTag." It will loop until it finds the END_TAG for <address>. If it encounters any other tag, then its name and the contents inside it are printed out to the console. So if the tag <name>Robert Cadena</name> is found, you'll see the following console output:

name: Robert Cadena

Once the end of <address> is found (lines 8-10) control is returned to the calling function, which then begins checking for <address> again.

As you can see, using the pull parser is easy, and it's a great benefit to be able to pass the parser to another function and begin looking for elements inside the document. And you aren't restricted to parsing resource files; you can also use HttpConnection and pass this function the http InputStream. This saves you from having to read the InputStream, save the content, and then parse it; kXML handles all that for you.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date