Java Reference
In-Depth Information
NanoXML
NanoXML is a very small XML parser developed by Marc De Scheemaecker. Eric Giguere has
provided a CLDC port of version 1.6.4 of the parser. The parser parses the whole XML document to a
memory structure that is accessible via the NanoXML API. The advantage of this approach is that
access to the document content is extremely simple. The trade-off is that the device needs enough heap
memory to hold the whole document structure. It isn't possible to access the XML document until it has
been read completely, so progressive display isn't possible.
The NanoXML API consists of only two classes: kXMLElement and XMLParseException (an
exception class). An XML document is parsed by creating an kXMLElement and then invoking
parseFromReader() with a reader as parameter. A kXMLElement object can be written to a
writer or a string using the write() method. Further important methods are
enumerateChildren() , which returns an Enumeration for iterating through all child elements,
and getContents() , which returns the text content of the given kXMLElement . For complete
NanoXML reference, refer to the NanoXML JavaDOC, available at http://nanoxml.sourceforge.net .
As an illustrative example for parsing XML, we will use the NewsForge XML format. NewsForge is a
news Web site with a focus on open source development, and it provides a URL where you can
download the current content in XML format. The NewsForge XML code consists of a
<backslash> element containing a number of <story> elements. The child elements of <story>
contain information about the story such as title, the author, and possibly a short description. Listing
10.1 shows an example of the NewsForge XML code. If you would like to run the demo but cannot
access NewsForge, you can use the example XML code for testing. For more details about NewsForge,
refer to http://www.newsforge.com .
Listing 10.1 newsforge.xml —NewsForge XML Code Example
<?xml version="1.0" encoding="iso-8859-1"?>
<backslash xmlns:backslash="http://www.newsforge.com/backslash.dtd">
<story>
<title>Linux 2.4.6-ac2</title>
<url>http://www.newsforge.com/article.pl?sid=01/07/07/169250</url>
<time>2001-07-07 16:08:37</time>
<author>tina</author>
<department></department>
<topic>gnulinux</topic>
<comments>0</comments>
<section>newsvac</section>
<image>topicnews.gif</image>
<description>&quot;Drop out various bits that
are 2.5 stuff...&quot;</description>
</story>
<story>
<title>Millions are shut out of Microsoft's
instant-messaging service</title>
<url>http://www.newsforge.com/article.pl?sid=01/07/07/0854238</url>
<time>2001-07-07 12:04:23</time>
<author>cdlu</author>
<department></department>
<topic>closedsrc</topic>
<comments>0</comments>
<section>newsvac</section>
<image>topicnews.gif</image>
<description>The Seattle Post-Intelligencer reports that
Microsoft's
instant-messaging service has been inaccessible
 
Search WWH ::




Custom Search