InputStream is = openHTTPConnection("blahblah");
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
Document doc = null;
try {
builder = fac.newDocumentBuilder();
doc = builder.parse(is);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
doc.getDocumentElement().normalize();
NodeList parentNodes = doc.getElementsByTagName("Parent");
for (int i = 0; i < parentNodes.getLength(); i++){
Node itemNode = parentNodes.item(i);
if (itemNode.getNodeType() == Node.ELEMENT_NODE){
Element parentElement = (Element) itemNode;
NodeList childNodes = ??????
}
}
我的 XML 文件:
<Blah>
<Parent>
<Child>
...
</Child>
</Parent>
</Blah>
如何获取 Parent 的子元素?教程说NodeList childNodes = (parentElement).getElementsByTagName("Child");
但这对我来说没有意义。
看起来我的帖子主要是代码;但我不知道要添加什么