Java Reference
In-Depth Information
The fromXml method still takes a string, but instead of obtaining a factory for SAX
parsers and creating a SAX parser, it only needs to create an instance of LocationParser ,
convert the incoming string to a byte array stream, and then invoke the LocationParser 's
parse method.
Note kXML isn't the only XML parser suitable for use with Java ME applications; others include NanoXML
and Xparse-J. Personally, I prefer kXML for its clean interface, widespread use, size, and stability, but if
you're looking for other options, it's worth using Google to find suggestions.
Wrapping Up
Interfacing with a web service is a key part of many Java ME applications. While Java ME
itself doesn't provide much support for accessing web services, with a bit of work you can
bridge the gap.
Web services require you to represent data as XML for exchange with the remote
web service over HTTP; while you can easily do the data exchange using the GCF and
HttpConnection , handling the XML requires you to do a little more. Encoding XML is
straightforward, typically a matter of building up an XML document using a combination
of static defined strings and field values from the object that you want to represent as
XML. You can do this reasonably efficiently using a StringBuffer and invoking its toString
method upon completion to obtain the XML document.
You have a number of alternatives available to you when you need to parse XML;
two of the most commonly available are the XML parser defined by the optional J2ME
Web Services Specification ( JSR 172) and the kXML parser.
JSR 172 defines both an optional RMI-style interface to remote web services that use
WSDL and SOAP as well as an optional SAX parser. Using the SAX parser, you can parse
XML by providing the SAX parser with a handler that processes the events the SAX parser
generates as it scans an XML document.
The kXML parser is packaged as a JAR file that you can include in your application;
it offers a simple pull parser you use by iterating over the XML tokens in your document.
You accomplish this iteration using the parser's methods, invoking your own code to
handle the tokens that result. One good way to use the kXML parser is to use it to gener-
ate events similar to the SAX parser. This gives you a reasonable level of abstraction
between the document-scanning portion of your parser (the responsibility of the kXML
parser and the loop you use to obtain successive XML tokens) and the parsing portion
of your parser.
 
Search WWH ::




Custom Search