javaxmlxpathdom4j

XPath returns for one element but doesn't return the other?


I am using Java to extract values using XPath. I was able to extract elements under the element fields but the elements under records are not returned.

XML is as follows:

    <?xml version="1.0" ?>
    <qdbapi>
        <action>****</action>
        <errcode>0</errcode>
        <errtext>No error</errtext>
        <qid>****</qid>
        <qname>****</qname>
      <table>
        <fields>            
        <field id="19" field_type="text" base_type="text">
        </field>
        </fields>
        <records>
        <record>
        <f id="6">1</f>
        </record>
        </records>
    </table>
  </qdbapi>

Code below:

XMLDOMDocObj.selectNodes("//*[local-name()='fields']")//21 fields returned
XMLDOMDocObj.selectNodes("//*[local-name()='records']")//no records are returned

Solution

  • XML must have a single root element; yours has two: fields and records.

    Wrap them in a single common root to get the results you expect.

    Also, if your XML has no namespaces, there's no reason to defeat them. Instead of

    //*[local-name()='records']
    

    use

    //records
    

    See also