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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

And do both of your XPath's work for you now as predicted?
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
Works for me: //*[local-name()='records'] (Click through to see.)
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 <records>
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 minimal reproducible example for any additional issues you're having?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.