如何使用JavaXPathXpression从xml中读取特定的标记值

如何使用JavaXPathXpression从xml中读取特定的标记值,java,xml,xpath,tags,Java,Xml,Xpath,Tags,下面是我的xml: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <soapenv:Envelope > <soapenv:Header/> <soapenv:Body> <inv:GetInvoiceAuthenticationResponse> <inv:Header> <inv:Cu

下面是我的xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soapenv:Envelope >
   <soapenv:Header/>
   <soapenv:Body>
      <inv:GetInvoiceAuthenticationResponse>
         <inv:Header>

            <inv:Cuit>123</inv:Cuit>

            <inv:EmissionPoint>?</inv:EmissionPoint>

            <inv:InvoiceType>?</inv:InvoiceType>

            <inv:ProcessDate>?</inv:ProcessDate>

            <inv:NoOfInvoices>1</inv:NoOfInvoices>

            <inv:Result>?</inv:Result>
         </inv:Header>
         <inv:Invoices>
            <inv:Invoice>
               <inv:Concept>?</inv:Concept>
               <inv:DocumentType>?</inv:DocumentType>
               <inv:DocumentNumber>?</inv:DocumentNumber>
               <inv:InvoiceNumber>?</inv:InvoiceNumber>

               <inv:InvoiceDate>?</inv:InvoiceDate>
               <inv:ResultCode>A</inv:ResultCode>

               <inv:CAE>?</inv:CAE>

               <inv:CAEExpiryDate>?</inv:CAEExpiryDate>


            </inv:Invoice>
         </inv:Invoices>
      </inv:GetInvoiceAuthenticationResponse>
   </soapenv:Body>
</soapenv:Envelope>
关于Java中使用XPath查询名称空间中元素的可能方法,请参见本文。个人偏好因为我不是java爱好者,所以我会选择XPath唯一的解决方案。您案例的XPath如下所示:

/*[local-name()='Invoices'
    and namespace-uri()='namespace-uri-for-inv-prefix']
  /*[local-name()='Invoice'
      and namespace-uri()='namespace-uri-for-inv-prefix']
    /*[local-name()='ResultCode'
        and namespace-uri()='namespace-uri-for-inv-prefix']/text()
如果您认为可以安全地忽略名称空间,请省略
namespace-uri()
过滤器以简化(注意上面第二个链接中解释的风险):


它给你一个错误,或者它没有给你正确的输出?它没有给我想要的输出。它不会扔任何东西error@AnubhavJhalani您希望得到什么以及代码实际输出了什么?顺便说一句,XML看起来无效,因为没有声明任何前缀(或者它只是整个XML的一部分?),它是XML的一部分。我希望输出中有“A”,正如您看到的那样,“A”是用inv:ResultCode标记和“代码实际输出的内容”编写的?
/*[local-name()='Invoices'
    and namespace-uri()='namespace-uri-for-inv-prefix']
  /*[local-name()='Invoice'
      and namespace-uri()='namespace-uri-for-inv-prefix']
    /*[local-name()='ResultCode'
        and namespace-uri()='namespace-uri-for-inv-prefix']/text()
/*[local-name()='Invoices']/*[local-name()='Invoice']/*[local-name()='ResultCode']/text()