嗯,这是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,但它不起作用,我收到此消息:
线程“main”中的异常nu.xom.XPathException:XPath错误:XPath表达式使用未绑定的命名空间前缀soap
谢谢!
答案 0 :(得分:0)
Thanks to har07, it can be solved like this:
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);