There are a lot of XML parsers out there. You need to chose one based on your specific requiremets. here are some descriptions of them.
[1] Categories
|
[2] Avaiable Parsers
Xerces-J is that what I choose and I think it is good enough to get start. Get it from Apache Xerces Java.
[3] Parsers
SAX - Read-Only; Quite fast; extremely memory efficient; Not easy to use for complicate XML document First You need to create your own handler by extends com.xml.sax.helpers.DefaultHandler.
import org.xml.sax.*;
Reader XML XMLReader parser = XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser" ); // There's a name conflict with java.net.ContentHandler // so we have to use the fully package qualified name. org.xml.sax.ContentHandler handler = new FibonacciHandler(); parser.setContentHandler(handler); InputStream in = connection.getInputStream(); // Input stream InputSource source = new InputSource(in); parser.parse(source); System.out.println(); in.close(); connection.disconnect(); } catch (Exception e) { System.err.println(e); } |
DOM - Tree-based; Random-access; Quite memory intensive; Must use interfaces and factory methods
DOMParser parser = new DOMParser(); InputStream in = connection.getInputStream(); InputSource source = new InputSource(in); parser.parse(source); in.close(); connection.disconnect(); Document doc = parser.getDocument(); NodeList doubles = doc.getElementsByTagName("double"); Node datum = doubles.item(0); Text result = (Text) datum.getFirstChild(); System.out.println(result.getNodeValue()); } catch (Exception e) { System.err.println(e); } |
JDOM - Tree-based, More Intuitive
try { // Read the response InputStream in = connection.getInputStream(); SAXBuilder parser = new SAXBuilder(); Document response = parser.build(in); in.close(); connection.disconnect(); // Walk down the tree String result = response.getRootElement() .getChild("params") .getChild("param") .getChild("value") .getChild("double") .getText(); System.out.println(result); } catch (Exception e) { System.err.println(e); } |
Dom4j - Tree-based; Pure-Java; Integrate XPath and XSLT and optional DOM compatibility try { // Read the response InputStream in = connection.getInputStream(); SAXReader reader = new SAXReader(); Document response = reader.read(in); in.close(); connection.disconnect(); // Use XPath to find the element we want Node node = response.selectSingleNode( "/methodResponse/params/param/value/double" ); String result = node.getStringValue(); System.out.println(result); } catch (Exception e) { System.err.println(e); } |
Hope this can help!
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8585387/viewspace-219073/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/8585387/viewspace-219073/