gpt4 book ai didi

java - Android 中的 XML 解析问题

转载 作者:行者123 更新时间:2023-12-02 07:32:26 24 4
gpt4 key购买 nike

这是我的 XML。

 <results keywords="ipod" limit="25">
<products count="30">
<product merchant_price="179.99"
network_id="2"
name = "ipod"
retail_price="179.99" />
<product merchant_price="189.99"
network_id="3"
name="ipod16gb"
retail_price="189.99" />
</products>
</results>

从中我需要单独获取产品属性。为此,我已经编写了一个解析器。

String xml = parser.getXmlFromUrl(url); // getting XML from the given URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName("product");
// looping through all item nodes
for (int i = 0; i < 10; i++) {
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
Log.v("test",e.getAttribute("name"));
}

但是我无法正确获取值并获取 空指针异常 谁能指出我在这里做错了什么?

最佳答案

尝试一下

     Element e = (Element) nl.item(i);
NamedNodeMap attributes = e.getAttributes();
for (int a = 0; a < attributes.getLength(); a++)
{
Node theAttribute = attributes.item(a);
System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());
}

关于java - Android 中的 XML 解析问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12726411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com