0
 <invoice>
    <obs>
    <ob>
     <code>ABC</code>
    </ob>
    <ob>
     <code>123</code>
    </ob>
    </obs>
  </invoice>
<invoice>
    <obs>
    <ob>
     <code>DEF</code>
    </ob>
    </obs>
  </invoice>
</invoices>

Ok , my question is , I have that xml , which will come to me from external system ,it can have large number of 'invoice' nodes and one 'invoice' node can have large number of 'code' nodes. I want to read the 'code' nodes of all 'invoice' nodes and save them in an array like this : invoice[1].code[1]=ABC invoice[1].code[2]=123 invoice[2].code[1]=DEF . How to do this using XPathExpression in JAVA. Below is my xpath expression which is not working.

expr = xpath.compile("//invoices/invoice/obs/ob/code/text()");

Please give a general answer in case number of 'invoice' nodes and 'code' nodes are high

1 Answer 1

1

Your XML is not well structured. There is an <invoices> missing in the beginning. The proper structure is the following :

    <invoices>
        <invoice>
            <obs>
                <ob>
                    <code>ABC</code>
                </ob>
                <ob>
                    <code>123</code>
                </ob>
           </obs>
        </invoice>
        <invoice>
            <obs>
                <ob>
                    <code>DEF</code>
                </ob>
            </obs>
        </invoice>
    </invoices>

The xpath expression you defined is right. I tested with python (and lxml library) for speed with the following commands :

from lxml import etree
tree = etree.parse('yourfile.xml')
root = tree.getroot()
root.xpath('//invoices/invoice/obs/ob/code/text()')

and I got the array you want :

['ABC', '123', 'DEF']
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for correcting the xml. But my question is , I want to write an expression which will give me all the codes inside a particular 'invoice' node. "'//invoices/invoice/obs/ob/code/text()" is not satisfying my requirement.
root.xpath('//invoices/invoice[1]/obs/ob/code/text()') will give you all the codes inside the 1st 'invoice' node root.xpath('//invoices/invoice[1]/obs/ob[2]/code/text()') will give you the 2nd code inside the 1st 'invoice' node

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.