繁体   English   中英

来自Android中URL的Xml值

[英]Xml values from URL in android

下面的xml文件位于“ http://example.com/example.xml

<xml>

<result>
    <name>abc name</name>                       
    <first>568</first>
    <second>458</second>
    <third>185</third>
    <forth>236</forth>
    <last>974</last>
</result>

<result>
    <name>def name</name>                       
    <first>214</first>
    <second>456</second>
    <third>457</third>
    <forth>754</forth>
    <last>746</last>
</result>

</xml>

我想获取abc名称的第一,第二,第三,第四和最后一个值。 我如何从xml URL获取值?

您应该尝试使用DOM解析器。 您应该尝试使用DOM解析器。

NodeList nList = doc.getElementsByTagName(“result”);

System.out.println("----------------------------");

for (int temp = 0; temp < nList.getLength(); temp++) {

    Node nNode = nList.item(temp);

    if (nNode.getNodeType() == Node.ELEMENT_NODE) {

        Element eElement = (Element) nNode;

        if( eElement.getElementsByTagName("firstname").item(0).getTextContent().equals(“abc”))
        {
           System.out.println(“first” + eElement.getElementsByTagName(“first”).item(0).getTextContent());
           System.out.println(“second” + eElement.getElementsByTagName("nickname").item(0).getTextContent());
          //And so on           
         }

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM