XML String Question

Hi All,

I have a node(?) that looks like this:
<person name="Tom Smith">
My question is how do I get “person name” and “Tom Smith” into two separate strings?

Thanks.

XML Class (Desktop, console, web and iOS project types)
https://documentation.xojo.com/api/text/xml/xmldocument.html

https://documentation.xojo.com/api/data_types/string.html#string-split

So when I do:

Var xn1 as XMLNode
Var sString as String

sString = xn1.name //yields the value of "person", I would like the whole part of that string "person name"
sString = xn1.GetAttribute("name") //yields the value of "Tom Smith" which is fine.

Getting the correct name for the attribute is the problem at the moment.

This example from the Xojo docs might explain what is going on

<Player name="Bob" position="1B" />

The object is not ‘Player name’ - it is ‘Player’
And the Player object has two attributes : Name (Bob) , and position (1B)

You are able to get the name, because you know that there is an attribute called “name”

Ideally, there would be a way to get Xojo to tell you what attributes are available.
The current help says there is no way to do that.

BUT the help may be wrong.

GetAttributeNode(Name As String) As XMLAttribute

is explained with

Gets an XML attribute node of the attribute specified by Index position. Index is zero-based.

Now clearly, the example is showing Name as String as the parameter list, NOT a string.
So there is a small chance that the prototype is incorrect, and the explanation is correct.

IF so, you should be able to get the word 'name" using GetAttributeNode(0)

1 Like

you may look at the XPath search feature?
you could find all person nodes, and then read the name value.
https://documentation.xojo.com/api/text/xml/xmldocument.html#xmldocument-xql

Thanks for point this out @Jeff_Tullin
@MarkusR ~ it is possible that I will have to go down this road.

Thanks.

The dark secret about xml is that while it is very flexible and can contain just about anything, in order to do something useful with it you must know the structure beforehand. In your case, you have to anticipate that there will be an attribute called “name”. You don’t need to iterate the attributes, because you have to already know what to do with them and what they are called.

2 Likes