首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在java中解析xml字符串

在java中解析xml字符串
EN

Stack Overflow用户
提问于 2014-08-26 04:10:25
回答 1查看 166关注 0票数 0

我一直在尝试使用从其他帖子中找到的代码来解析xml字符串,但当尝试访问节点元素时,我总是得到空值。有人能看到错误或我遗漏的东西吗?

代码语言:javascript
复制
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以以下内容开头:

代码语言:javascript
复制
<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>...

编辑:

代码语言:javascript
复制
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());   
        }
EN

回答 1

Stack Overflow用户

发布于 2014-08-27 10:04:57

替换NodeList nodes = root.getElementsByTagName("entry");

作者:NodeList nodes = root.getChildNodes();

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25493605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档