[XML Parsing] Trouble getting tags value

Hi everyone,

I just want to say before I begin that i have, of course, looked at the different topics here about my matter AND read the documentation.

With that being said, my matter is that i want to get TAG values from XML String.
I insist on the TAG value and NOT attribute value.

Here is an exemple XML i use :

<?xml version="1.0" encoding="UTF-8"?>
<prestashop
  xmlns:xlink="http://www.w3.org/1999/xlink">
  <product>
    <id>
      <![CDATA[41]]>
    </id>
    <id_manufacturer></id_manufacturer>
    <id_supplier></id_supplier>
    <id_category_default></id_category_default>
    <new></new>
    <cache_default_attribute></cache_default_attribute>
    ...
    </product>
</prestashop>

And then, here’s the code I wrote so that i can get the ID value :

1. Var contentXML as new XmlDocument(content)
2. Var ids as XmlNodeList = contentXML.XQL("//id")
3. Var id as XmlNode = ids.Item(0)

So, with the first instruction, i import the XML which is in the “content” variable (String).
Secondly, I get all “id” nodes
Finally, i get the first “id” node.

Result :
< id>
< ![CDATA[41]]>
< /id>

THIS is exactly what I don’t want. How do i access the id node value ?

I’m taking this simple exemple here but i have XML with hundred of properties and i would like to parse all of them (or specific ones) and get their values. How can I do that?
The code I wrote was to get the closest possible to my need but I don’t need node or anything, I just want “//produt/id” value. I’ve seen TONS of topics explaining how to get attributes values, i don’t have any so it won’t help me

Thanks in advance for your help!

Almost there. The CDATA node is a child of the id node, and you need its value:

Var idValue As String = id.FirstChild.Value

Of course, you should consider testing FirstChild for Nil before accessing its property, or you could get a NilObjectException if you encounter an id that is not in the form you expect it, e.g., “” would throw the exception.

Thank you, you’re a genius

Thanks for the compliment. It is not really genius. I have spent many years working on an application that makes extensive use of Xojo’s XML features.

For completeness, I should point out that the CDATA element may not always be the FirstChild if the id element is not formed exactly as shown in your example. If the value you need is always in a CDATA, the more resilient way to get the value would be to loop over the children of your variable id looking for one that has the node type (XMLNodeType) of CDATA.

Good luck!