使用 dom4j 解析

发布于 2024-09-15 02:00:18 字数 549 浏览 2 评论 0原文

我成功地使用 xpath 表达式 /abcde/response 从 xml 检索 response 数据,

<abcde>
            <response>000</response>
</abcde>

但无法从同一个表达式检索 response 数据xml 但有一些附加数据

<abcde version="8.1" xmlns="http://www.litle.com/schema"
      response="0" message="Valid Format">
            <response>000</response>
</abcde>

我做错了什么?

I am successfully retrieve the data of response using xpath expression /abcde/response from the xml ,

<abcde>
            <response>000</response>
</abcde>

But couldnt retrieve the data of response from the same xml but with some additional data

<abcde version="8.1" xmlns="http://www.litle.com/schema"
      response="0" message="Valid Format">
            <response>000</response>
</abcde>

What am i doing wrong ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红尘作伴 2024-09-22 02:00:18
package stackoverflow;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.DocumentHelper;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import org.dom4j.xpath.DefaultXPath;
import org.jaxen.VariableContext;

public class MakejdomWork {
    public static void main(String[] args) {
        new MakejdomWork().run();
    }

    public void run() {
        ByteArrayInputStream bis = new ByteArrayInputStream("<abcde version=\"8.1\" xmlns=\"http://www.litle.com/schema\"      response=\"0\" message=\"Valid Format\">            <response>000</response></abcde>".getBytes());
        //ByteArrayInputStream bis = new ByteArrayInputStream("<abcde><response>000</response></abcde>".getBytes());

        Map nsPrefixes = new HashMap();
        nsPrefixes.put( "x", "http://www.litle.com/schema" );

        DocumentFactory factory = new DocumentFactory();
        factory.setXPathNamespaceURIs( nsPrefixes );

        SAXReader reader = new SAXReader();
        reader.setDocumentFactory( factory );
        Document doc;
        try {
            doc = reader.read( bis );
            Object value = doc.valueOf("/abcde/x:response");
            System.out.println(value);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}

简短的回答:如果你的解析器是命名空间感知的(dom4j 是),你需要使用命名空间前缀

package stackoverflow;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.DocumentHelper;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import org.dom4j.xpath.DefaultXPath;
import org.jaxen.VariableContext;

public class MakejdomWork {
    public static void main(String[] args) {
        new MakejdomWork().run();
    }

    public void run() {
        ByteArrayInputStream bis = new ByteArrayInputStream("<abcde version=\"8.1\" xmlns=\"http://www.litle.com/schema\"      response=\"0\" message=\"Valid Format\">            <response>000</response></abcde>".getBytes());
        //ByteArrayInputStream bis = new ByteArrayInputStream("<abcde><response>000</response></abcde>".getBytes());

        Map nsPrefixes = new HashMap();
        nsPrefixes.put( "x", "http://www.litle.com/schema" );

        DocumentFactory factory = new DocumentFactory();
        factory.setXPathNamespaceURIs( nsPrefixes );

        SAXReader reader = new SAXReader();
        reader.setDocumentFactory( factory );
        Document doc;
        try {
            doc = reader.read( bis );
            Object value = doc.valueOf("/abcde/x:response");
            System.out.println(value);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}

Short answer: you need to use namespace prefixes if your parser is namespace aware (which dom4j is)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文