0

Below is my 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>

I want to read the inv:ResultCode tag . I am using below expression but it's not working. Please tell what I am doing wrong?

Which path I should give in xpath.compile?

expr = xpath.compile("//inv:Invoices/inv:Invoice/inv:ResultCode/text()");
Cœur
  • 37,241
  • 25
  • 195
  • 267
Anudocs
  • 686
  • 1
  • 13
  • 54
  • It gives you an error or it doesn't give you the right output? – ZwoRmi Mar 20 '15 at 12:02
  • it doesnt give me the desired output. It doesnt throw any error – Anudocs Mar 20 '15 at 12:09
  • @AnubhavJhalani What do you expect to get and what the code actually outputted? Btw, the XML looks invalid as none of the prefixes are declared (or is it only part of the entire XML?). – har07 Mar 20 '15 at 12:11
  • its a part of the xml . I expect 'A' in the output as you can see 'A' is written in inv:ResultCode tag – Anudocs Mar 20 '15 at 12:15
  • And "what the code actually outputted"? – ZwoRmi Mar 20 '15 at 12:18
  • result1=expr.evaluate(dc, XPathConstants.NODESET); nodes =(NodeList)result1; System.out.println("Number of matching nodes:"+nodes.getLength()); It gives 0 length – Anudocs Mar 20 '15 at 13:10

1 Answers1

0

See this other SO post on possible ways, in Java, to query elements in namespace using XPath. Personal preference since I'm not a java guy, I would go with XPath only solution using local-name() and namespace-uri(). The XPath for your case is as follow :

/*[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()

And if you think you're safe to ignore the namespace, omit namespace-uri() filters to simplify (note the risk as explained in the 2nd link above) :

/*[local-name()='Invoices']/*[local-name()='Invoice']/*[local-name()='ResultCode']/text()
Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137