In this tutorial, we aim to build an XML parser that processes an RSS feed into an array of strings, rather than a single string.
public class AndroidSAXExample { public Channel parse(InputStream is) { RootElement root = new RootElement("rss"); Element chanElement = root.getChild("channel"); // Process elements with listener methods chanElement.setStartElementListener(new StartElementListener() { ... }); chanElement.setEndTextElementListener(new EndTextElementListener() { ... }); // Parse input stream and return Channel object Xml.parse(is, Xml.Encoding.UTF_8, root.getContentHandler()); return channel; } }
public class SAXExampleHandler extends DefaultHandler { @Override public void startElement(...) { ... } @Override public void endElement(...) { ... } @Override public void characters(...) { ... } @Override public void endDocument() { ... } }
Android SAX:
org.xml.sax:
The choice between the two implementations depends on the complexity of your XML structure. For simpler structures, the Android SAX implementation is preferred. For more complex structures, the org.xml.sax implementation provides greater flexibility at the cost of additional coding complexity.
The above is the detailed content of How to Choose Between Android SAX and org.xml.sax for XML Parsing?. For more information, please follow other related articles on the PHP Chinese website!