1

好吧,这是xml:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<FECompUltimoAutorizadoResponse xmlns="http://ar.gov.afip.dif.FEV1/">
<FECompUltimoAutorizadoResult>
<PtoVta>12</PtoVta>
<CbteTipo>1</CbteTipo>
<CbteNro>1</CbteNro>
</FECompUltimoAutorizadoResult>
</FECompUltimoAutorizadoResponse>
</soap:Body>
</soap:Envelope>

我想获得 CbteNro,但我不能因为 FECompUltimoAutorizadoResponse,我尝试了这个:

Document document = new Builder().build(responseString, "test.xml");
    Nodes nodes = document.query("/soap:Envelope[@xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"]/soap:Body/FECompUltimoAutorizadoResponse[@xmlns=\"http://ar.gov.afip.dif.FEV1/\"]/FECompUltimoAutorizadoResult/CbteNro\n");
    System.out.println(nodes.get(0).getValue());

使用 XOM,但它不起作用,我收到此消息:

线程“主”nu.xom.XPathException 中的异常:XPath 错误:XPath 表达式使用未绑定的命名空间前缀 soap

谢谢!

4

1 回答 1

0

感谢har07,可以这样解决:

Document document = new Builder().build(responseString, "test.xml");
Element rootElem = document.getRootElement();
XPathContext xc = XPathContext.makeNamespaceContext(rootElem);
xc.addNamespace("fev1", "http://ar.gov.afip.dif.FEV1/");
Nodes matchedNodes = rootElem.query("/soap:Envelope/soap:Body/fev1:FECompUltimoAutorizadoResponse/fev1:FECompUltimoAutorizadoResult/fev1:CbteNro", xc);
于 2015-08-07T02:12:10.960 回答