我一直在尝试使用从其他帖子中找到的代码来解析xml字符串,但当尝试访问节点元素时,我总是得到空值。有人能看到错误或我遗漏的东西吗?
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(post));
Document doc;
try {
doc = db.parse(is);
doc.getDocumentElement().normalize();
Element root = doc.getDocumentElement();
NodeList nodes = root.getElementsByTagName("entry");
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}根元素为空,节点列表也为空。XML post以以下内容开头:
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:activity="http://activitystrea.ms/spec/
1.0/" xmlns:service="http://activitystrea.ms/
service-provider" xmlns:thr="http://purl.org/syndication/thread/1.0"
xmlns:gnip="http://www.post.com/schemas/2010"
xmlns:geo="http://www.georss.org/georss" xmlns:poco="http://portablecontacts.net/spec/1.0">
<id>...编辑:
NodeList nodes.. //The value of the local nodes is not used
[entry: null]
// it skips the following lines...
for(int i=0; i < nodes.getLength() - 1; i++){
System.out.println(nodes.item(i).toString());
}发布于 2014-08-27 10:04:57
替换NodeList nodes = root.getElementsByTagName("entry");
作者:NodeList nodes = root.getChildNodes();
https://stackoverflow.com/questions/25493605
复制相似问题