Parsing an xml Document

Hi people.
I’ve been messing about for a good while now and being blocked by something that must be really elementary, but that I"m not seeing :frowning:. I’m trying to parse an xml file structured as follows:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<TheRoot>
  <MessageHeader>
    <MessageId>20240602012345</MessageId>
    <MessageSender>
      <PartyID>1234</PartyID>
      <PartyName>My Company Name</PartyName>
    </MessageSender>
    <MessageRecipient>Recepient Name</MessageRecipient>
  </MessageHeader>
  <MessageDates>
    <StartDate>2024-06-02</StartDate>
    <EndDate>2024-06-02</EndDate>
  </MessageDates>
  <Entry1>
    <UserId>01234567890123</UserId>
    <EntryId>
      <Eno>ABC0123456</Eno>
    </EntryId>
    <Title>This is the Title</Title>
  </Entry1>
  <Comment>Some sort of comment</Comment>
  <NumberOfEntries>1</NumberOfEntries>
</TheRoot>

There’s only 1 “Entry” (Entry1) in my example, but in practice there could be dozens! I will be using loops and all to deal with those, but my problem is to access a given value, for instance, If I want to read Entry1/Eno, I would hope to get “ABC0123456”.
I do something like:

Var nodeIwant As XmlNode = xml.DocumentElement.Child(2).Child(1).Child(0)
Var txt As Variant = nodeIwant.Value
MessageBox nodeIwant.ToString

I get ABC0123456 with the tags, not just the text.
What am I doing wrong ? I’ve messes about with all sorts of combinations, but I’m not getting it.
Can someone there help me please ? Thanks in advance.

Please don’t just hard code Child() calls like this.

Best may be XPath query. Or write loops to check node by name.
See XQL function for the query.

2 Likes
Var xml As New XMLDocument
xml.LoadXML(textXML.Text)
Var nodeIwant As XmlNode = xml.DocumentElement.Child(2).Child(1).Child(0)
If nodeIwant.FirstChild IsA XMLTextNode Then
  System.DebugLog "nodeIwant.FirstChild IsA XMLTextNode"
End If
Var txtNode As XMLTextNode = XMLTextNode(nodeIwant.FirstChild) 
Break
MessageBox txtNode.Value

press play after break,
a break point is useful to see the object properties in debug window.

I may add more from the answer Norman suggested:

Dont try & parse it manually

Use an XPath query (at least)

http://xpather.com/

try Path

/TheRoot/Entry1/EntryId/Eno

I’d suggest

  1. get the NumberOfEntries
  2. iterate over all those with revised XPath queries for each one
2 Likes

Actually xql is something ChatGPT is pretty good at.

Try giving it your xml and asking for what you want in Xojo xql code, should get you started.

Hi Christian. Thanks for your answer. I did say that I would be using loops in my post :wink:
I’m not sure what a XPath query is, but I’ll check it out.
The child calls were only to try to show as clearly as possible, what was blocking me :+1:t2:

Very interesting. I would try that but I don’t use ChatGPT al all :joy:.
Thanks for your answer Michelle.

Thanks for your reply Marcus. That’s just what I was missing​:+1:t2:. I didn’t quite dig deep enough :joy:. I have now tried your way and it works just fine.

Thanks to you and to all who answered :smiley:

1 Like