如何使用包含Java中的命名空间的XPath检索XML数据? [英] how to retrieve XML data using XPath which contains namespace in Java?

查看:124
本文介绍了如何使用包含Java中的命名空间的XPath检索XML数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个主题有很多这个主题,但遗憾的是,我仍然无法得到我的解决方案..

i know there are plenty of this topic in this page but sadly, i still cant get my solution..

这是我的xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<ns1:Request xmlns:ns1="http://www.sea.com">
<ns1:PayrollRequest>
  <ns1:PayrollCost>
    <ns1:PayrollID>123</ns1:PayrollID>
    <ns1:BatchID>7770</ns1:BatchID>
    <ns1:CompanyId>001</ns1:CompanyId>
    <ns1:GrossPay>60000</ns1:GrossPay>
  </ns1:PayrollCost>
</ns1:PayrollRequest>
</ns1:Request>

这是我在java中的代码:

and this is my code in java:

import org.w3c.dom.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import java.io.IOException;
import org.xml.sax.SAXException;

public class XPathTry {

public static void main(String[] args) 
throws ParserConfigurationException, SAXException, 
IOException, XPathExpressionException {

DocumentBuilderFactory domFactory = 
DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); 
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("SamplePayroll2.xml");
XPath xpath = XPathFactory.newInstance().newXPath();


// display all
XPathExpression expr = xpath.compile("//PayrollCost/*/text()");


Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue()); 
   }
  }
}

ya,像往常一样,我无法得到输出,因为它只显示:

ya, as usual, i cant get the output as it only displays:

Process exited with exit code 0.

输出只会在我删除ns:1时显示,其中xml的代码将是这样的:

the output will only display when i remove the ns:1 which the code for the xml will be like this:

<?xml version="1.0" encoding="UTF-8"?>
<Request xmlns:ns1="http://www.sea.com">
<PayrollRequest>
    <PayrollCost>
        <PayrollID>123</PayrollID>
        <BatchID>7770</BatchID>
        <CompanyId>001</CompanyId>
        <GrossPay>60000</GrossPay>
    </PayrollCost>
</PayrollRequest>
</Request>

问题是,我在net none中找到的所有建议似乎都在起作用:

The problem is, all the suggestions i found in the net none seems to be working:

例如,我已经尝试了

/*/namespace::*[name()='']

//*[local-name() = 'Element' and namespace-uri() = namespace-uri(/*)]

/*[local-name()=' ']/*[local-name()=' ']/*[local-name()=' ']

etc2 ..

我能得到的唯一最佳输出是,它会显示:

the only best output i can get is, it will display:

null

有人能给我正确的代码吗?

can anyone give me the correct code for my problem?

先谢谢!

推荐答案

您将不得不创建 javax.xml.namespace.NamespaceContext 并将其设置为 xpath

You are going to have to create a subclass of javax.xml.namespace.NamespaceContext and set it on xpath:

xpath.setNamespaceContext(new NamespaceContext() {

    @SuppressWarnings("rawtypes")
    @Override
    public Iterator getPrefixes(final String namespaceURI) {
        return Collections.singleton("ns1").iterator();
    }

    @Override
    public String getPrefix(final String namespaceURI) {
        return "ns1";
    }

    @Override
    public String getNamespaceURI(final String prefix) {
        return "http://www.sea.com";
    }
});

然后您可以将命名空间前缀添加到XPath表达式中:

Then you can add the namespace prefix to the XPath expression:

XPathExpression expr = xpath.compile("//ns1:PayrollCost/*/text()");

这篇关于如何使用包含Java中的命名空间的XPath检索XML数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆