1

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
kjhughes
  • 106,133
  • 27
  • 181
  • 240
Onnesh
  • 1,169
  • 3
  • 15
  • 31

1 Answers1

1

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

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • And do both of your XPath's work for you now as predicted? – kjhughes Dec 16 '19 at 19:45
  • Only one of the Xpath values work namely 'fields' but the 'records' Xpath didnt work. Your suggestion for '//records' didn't work as well. For fields, the result is QBQueryResult:21 but for records the result is QBQueryResult:0. "QBQueryResult" is my result count – Onnesh Dec 16 '19 at 19:49
  • Works for me: [`//*[local-name()='records']`](http://xpather.com/okqmVUE7) (Click through to see.) – kjhughes Dec 16 '19 at 19:50
  • Yes, this worked!!! //*[local-name()='records']. However both did not work..//*[local-name()='records']/*[local-name()='record']. Also //*[local-name()='records']//record did not work. Xpath tester online returns no error. there are at-least 4 records under – Onnesh Dec 16 '19 at 20:01
  • Both XPaths listed in my answer will work with your (corrected) XML. So will `//records/record`. Since your original problem is solved, I may I suggest that you accept this answer and post a new question with a complete [mcve] for any additional issues you're having? – kjhughes Dec 16 '19 at 20:18