如何使用包含Java中的命名空间的XPath检索XML数据?

时间:2022-05-25 05:33:25

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

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

here is my xml code:

这是我的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>

and this is my code in java:

这是我在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, as usual, i cant get the output as it only displays:

雅,像往常一样,我不能得到输出,因为它只显示:

Process exited with exit code 0.

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

输出只会在我删除ns:1时显示,其中xml的代码将如下所示:

<?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>

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

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

for example, i already tried the

例如,我已经尝试过了

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

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

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

etc2..

ETC2 ..

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

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

null

can anyone give me the correct code for my problem?

谁能给我正确的代码来解决我的问题?

Thanks in Advance!

提前致谢!

2 个解决方案

#1


4  

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

您将不得不创建javax.xml.namespace.NamespaceContext的子类并在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";
    }
});

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

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

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

#2


1  

You need to use a NamespaceContext for your XPath expression. You can read more about how to do this here

您需要为您的XPath表达式使用NamespaceContext。您可以在此处详细了解如何执行此操作

#1


4  

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

您将不得不创建javax.xml.namespace.NamespaceContext的子类并在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";
    }
});

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

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

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

#2


1  

You need to use a NamespaceContext for your XPath expression. You can read more about how to do this here

您需要为您的XPath表达式使用NamespaceContext。您可以在此处详细了解如何执行此操作