Which API should I use to parse XML response from a URL stored in 开发者_StackOverflow社区a string buffer ?
Use JAXB:
// Given StringBuffer sb from your URL
String contextPath = "path/to/generated/jaxb/package";
JAXBContext jc = JAXBContext.newInstance(contextPath);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Object o = u.unmarshal(new StreamSource( new StringReader(sb.toString())));
It depends on how you want to process the XML.
If it is a large document and you just want to perform a one pass parse from head to tail then use a SAX API. This method will use less memory and have higher processing throughput.
If you want to manipulate the XML tree, and traverse the Nodes up and down the tree then use a DOM API. This method requires the whole XML DOM to be in memory, but allows you to manipulate the DOM and perform XPath type queries on it.
I believe SAXON can handle both.
精彩评论