java:带有命名空间的xpath表达式

时间:2021-01-20 15:51:29

标签: java xml xpath

我正在尝试使用此代码提取 xml 的一部分:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(responseStream);

XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//[local-name()='UpdateResponseDocument']");

如您所见,我使用的是 local-name

我收到此异常:

<块引用>

引起:javax.xml.transform.TransformerException:在“/”或“//”标记之后需要一个位置步骤。

我的 xml 是:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ns2:cercarInformacioPacientResponseV2 xmlns:ns2="http://salut.gencat.cat/hc3/etccerques" xmlns:ns3="urn:hl7-org:v3" xmlns:ns4="http://principal.agenda.consulta.ws.hc3.salut.cat/">
      <ns2:UpdateResponseDocument>
        <idMissatge>123456789
        </idMissatge>
        <resultCode>OK
        </resultCode>
        <result>
          <executionCode>ETC_OIP_000
          </executionCode>
          <executionDetails>El procés s'ha dut a terme correctament.
          </executionDetails>
          <pacient>
            ...
          </pacient>
        </result>
      </ns2:UpdateResponseDocument>
    </ns2:cercarInformacioPacientResponseV2>
  </soap:Body>
</soap:Envelope>

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

尝试更换

"//[local-name()='UpdateResponseDocument']"

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

注意星号 * 代表任何元素节点

相关问题