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


advertisement
 

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

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
DOM Processing
Pull parsing is great when you need to maintain a very low memory overhead because only the part of the document that issued that event is present. In other words, if the particular piece of data you're interested in is several hundred bytes in the middle of the document, the previous hundred bytes don't need to reside in memory.

But if you can afford to spare some memory you can use the other version of the kXML parser, which contains support for a DOM. A DOM is the entire document tree kept in memory with each tag separated into Node objects. You can traverse this document "tree" and get data as needed.

The other MIDlet in the project, kXMLDemo_dom, does the same thing. It reads an address book and prints the contents to the console, but this time it uses the DOM. Shown below are some of the more important lines in that file.



1.Document doc = new Document(); 2.... 3.parser = new XmlParser( isr ); 4.doc.parse( parser );

Line 1 creates a document, which will hold the XML tree. Line 3 creates a kXML parser from an InputStreamReader named isr. Line 4 passes the parser to the document and tells the document to begin parsing. The XML is recursively parsed until END_DOCUMENT is reached. When the call to parse exits the entire document has been loaded into memory and you can now manipulate it.

1.Element root = doc.getRootElement(); 2.int child_count = root.getChildCount(); 3.... 4.for (int i = 0; i < child_count ; i++ ) { 5.... 6. Element kid = root.getElement(i); 7. 8. if (!kid.getName().equals("address")) { 9. continue; 10. }

Because we know that <address> elements are direct children of the root element we can traverse the children of the root element and look for the address tag, looping back if the child is not an address tag.

1.int address_item_count = kid.getChildCount(); 2. 3. for (int j = 0; j < address_item_count ; j++) { 4....

If we've gotten this far then the element is an address and we start traversing its children to look for elements and print out their content. Unfortunately you can't just say kid.getElement("name") because if the element does not exist you'll get a RuntimeException. So I would suggest only using such methods when you know that all required fields in the XML document are present.

Minding Your Memory
As a rule of thumb you should use the pull parser when you're not sure of the structure of your application and you need to keep memory down. Use the DOM when you have enough memory and perhaps you need to manipulate the existing document by adding or removing tags.

If you want to see the difference in memory usage between the two methods, open up the project in KtoolBar and enable the Memory Monitor (Edit --> Preferences: Monitoring Tag). When you run the MIDlet you'll see the memory monitor window popup and the amount of memory along with a graphical representation of the same data. Run each of the applications and watch the green line rise. This represents the amount of memory your app is consuming. You'll see that the pull application uses less memory than the DOM application. Although the difference might not be that great between the two MIDlets in this example, a MIDlet that retrieves a larger file will use more memory if parsed into a DOM.

Getting kXML
kXML project's page is located at http://kxml.enhydra.org. You can download the JAR file with or without DOM support so that you only have to provide exactly the classes you need and leave the optional classes out.



Robert Cadena is owner of factorysouth, a Web and wireless application development company for the entertainment industry. You can reach him at . Reach him by e-mail .
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap