如何在android中获取xml的子元素? InputStream is=openHTTPConnection(“blahblah”); DocumentBuilderFactory fac=DocumentBuilderFactory.newInstance(); 文档生成器; 单据单据=空; 试一试{ builder=fac.newDocumentBuilder(); doc=builder.parse(is); }捕获(ParserConfiguration异常e){ e、 printStackTrace(); }捕获(SAXE异常){ e、 printStackTrace(); } doc.getDocumentElement().normalize(); nodelistparentnodes=doc.getElementsByTagName(“父”); 对于(int i=0;i

如何在android中获取xml的子元素? InputStream is=openHTTPConnection(“blahblah”); DocumentBuilderFactory fac=DocumentBuilderFactory.newInstance(); 文档生成器; 单据单据=空; 试一试{ builder=fac.newDocumentBuilder(); doc=builder.parse(is); }捕获(ParserConfiguration异常e){ e、 printStackTrace(); }捕获(SAXE异常){ e、 printStackTrace(); } doc.getDocumentElement().normalize(); nodelistparentnodes=doc.getElementsByTagName(“父”); 对于(int i=0;i,android,Android,我的XML文件: InputStream is = openHTTPConnection("blahblah"); DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; Document doc = null; try { builder = fac.newDocumentBuilder(); doc = builder.parse(is); } ca

我的XML文件:

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 = ??????
   }
}

...
如何获取Parent的子元素?教程说,
nodelistchildnodes=(parentElement.getElementsByTagName(“Child”)但这对我来说没有意义


看起来我的帖子主要是代码;但我不知道要添加什么,教程是正确的。这就是你得到它的方式:

<Blah>
   <Parent>
      <Child>
         ...
      </Child>
   </Parent>
</Blah>

如果有多个
节点,可以循环遍历迭代器,这样就可以在android中获得xml的子元素

您可以编写一个xsl

NodeList childNodes = (parentElement).getElementsByTagName("Child");

Element childElement=(Element)childNodes.item[0];

在xml上运行xsl,输出将是第1节的链接1 is ImageLink 1。。第2节中的链接4是图像链接2

但它不起作用,它说:类型元素的方法getElementsByTagName(String)未定义。你能发布完整的源代码吗,该方法存在:我发现了错误,我导入了错误的类。现在它起作用了。非常感谢你。但是parentElement周围的括号仍然让我困惑..是的,您可以删除括号
<xsl:template match="/">
  <xsl:for-each select="section">
    <xsl:value-of select="concat('link ',photo@id, ' from ',@name,' is ',photo@ilink)"><xsl:value-of> 
  </xsl:for-each>
</xsl:template>
run the xsl on your xml, the output will be link 1 from section1 is ImageLink 1 .. link 4 from section2 is ImageLink 2