在下面的代码中,当我使用 getNodeValue() 时,我什么也没得到,但是当我使用 getTextContent() 时,它会返回它的内容以及它的子内容。谁能告诉我为什么 getNodeValue() 不起作用?XML 在这里给出:
http://developer.android.com/training/basics/network-ops/xml.html
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://stackoverflow.com/feeds/tag?tagnames=android&sort=newest");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(is, null);
NodeList rootNode = doc.getElementsByTagName("entry");
for (int i = 0; i < rootNode.getLength(); i++)
{
Node node = rootNode.item(i);
if(node.getNodeType()==Node.ELEMENT_NODE){
Element element = (Element)node;
String title = element.getElementsByTagName("title").item(0).getTextContent();
String author = element.getElementsByTagName("author").item(0).getNodeValue();
String summary = element.getElementsByTagName("summary").item(0).getNodeValue();
String result = title+"\n"+author+"\n"+summary+"";
list.add(result);
}
}