Document doc = getDomElement(response); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
String name = getValue(e, KEY_NAME);
String description = getValue(e, KEY_DESC);
Log.e("description:", description);
}
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
public final String getElementValue(Node elem) {
Node child;
if (elem != null) {
if (elem.hasChildNodes()) {
for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
if ((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.ELEMENT_NODE) ) {
return child.getNodeValue();
}
}
}
}
return "";
}
在上面,响应是一个 XML RSS 提要,下面是一个子项。发生的事情是我能够获得标题、发布、更新。但是当我使用 getValue(e, "content") 我得到空字符串。我也想得到作者的名字。
<entry>
<title>Title1</title>
<link rel="alternate" type="text/html" href="http://www.example.com" />
<id>ID</id>
<published>2012-09-08T18:45:40Z</published>
<updated>2012-09-08T18:43:01Z</updated>
<author>
<name>Author name</name>
<uri>http://www.example.com</uri>
</author>
<content type="html" xml:lang="en" xml:base="http://www.example.com/">
<p>Test Test</content>
</entry>