使用JDOM,获取InputStream并将其设为document:
InputStream inputStream = (InputStream)httpURLConnection.getContent();documentBuilderFactory docbf = documentBuilderFactory.newInstance();docbf.setNamespaceAware(true);documentBuilder docbuilder = docbf.newdocumentBuilder();document document = docbuilder.parse(inputStream, baseUrl);
那时,您在Java对象中具有XML。做完了 简单。
您既可以使用文档对象和Java API来遍历它,也可以使用XPath,我发现它很容易(一旦我了解了它)。
构建一个XPath对象,这需要一点时间:
public static XPath buildXPath() { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); xpath.setNamespaceContext(new AtomNamespaceContext()); return xpath;}public class AtomNamespaceContext implements NamespaceContext { public String getNamespaceURI(String prefix) { if (prefix == null) throw new NullPointerException("Null prefix"); else if ("a".equals(prefix)) return "http://www.w3.org/2005/Atom"; else if ("app".equals(prefix)) return "http://www.w3.org/2007/app"; else if ("os".equals(prefix)) return "http://a9.com/-/spec/opensearch/1.1/"; else if ("x".equals(prefix)) return "http://www.w3.org/1999/xhtml"; else if ("xml".equals(prefix)) return XMLConstants.XML_NS_URI; return XMLConstants.NULL_NS_URI; } // This method isn't necessary for XPath processing. public String getPrefix(String uri) { throw new UnsupportedOperationException(); } // This method isn't necessary for XPath processing either. public Iterator getPrefixes(String uri) { throw new UnsupportedOperationException(); }}然后只需使用它(幸运的是)根本不需要太多时间:
return Integer.parseInt(xpath.evaluate("/a:feed/os:totalResults/text()", document));


