Parsing XML without named attribute values

I am getting xml from a website in the following format…

<?xml version="1.0" encoding="UTF-8"?>
    <schools>
        <details>
            <name>Academy of the Pacific</name>
            <id>1</id>
           <districtid>1</districtid>
           <rank>0</rank>
       </details>
      <details>
          <name>Ahuimanu Elementary</name>
         <id>107</id>
         <districtid>6</districtid>
         <rank>0</rank>
      </details>
   </schools>

If I use the XMLReader class I don’t see how to get the values. In the example they use StartElement…

[code]Sub StartElement(name as String, attributeList as XmlAttributeList)
Select Case name
Case “Team”
// On the Team node, get the name attribute and
// display it
// OutPut is a ListBox
Output.AddRow(attributeList.Value(“name”))

Case “Player”
// Get each player node and display the player name
// and position
// Output is a ListBox
Output.AddRow("", attributeList.Value(“name”), attributeList.Value(“position”))

End Select
End Sub
[/code]

But that is parsing XML in the following format where each attribute contains named values…

<?xml version="1.0" encoding="UTF-8"?>
<League>
	<Team name="Seagulls">
		<Player name="Bob" position="1B" />
		<Player name="Tom" position="2B" />
	</Team>
	<Team name="Pigeons">
		<Player name="Bill" position="1B" />
		<Player name="Tim" position="2B" />
	</Team>
	<Team name="Crows">
		<Player name="Ben" position="1B" />
		<Player name="Ty" position="2B" />
	</Team>
</League>

The attributes in the xml I am getting does not have any named values. I took a look at using the XMLDocument class, but don’t see how to do it there. I need to get the name and id values into a popup menu.

Thanks,

John

I believe you want the Characters event for the data between the tags.

Greg, Thanks.

I can’t figure out how to get the Characters. Using the XMLReader project that came with the xojo download as an example to work with, I created an XMLReader subclass with StartElement and Characters event handlers.

For testing purposes and being that my xml is basically key value pairs, I gave my XMLReader subclass a dictionary to output the key value pairs to and a string property called “key”.

In the StartElement event handler I set “key = name” and in the Characters event I updated the dictionary with “Output.Value(key)=s”.

The dictionary is getting the Key but the value is blank. The parameter s in the Characters event is always an empty string.

My approach must be flawed. I cannot find any examples of how to access the “characters” using XMLReader.

Any help would be greatly appreciated.

Ok. I can see now what is happening. The parser fires several times on the same element. On one of those firings Characters gets what is between the tags on all the other hits it gets what looks like a carriage return or a line feed. The s string has 2 lines with nothing on the lines.

What’s up with that.

I have it working now. I am now testing to see if the property Key is “” and if not adding the item to the dictionary then setting key back to “”. In this way I am only updating the dictionary immediately after StartElement has set Key to the element name. The first hit appears to always have s populated properly.

StartElement is in fact firing only once for each element. Characters, on the other hand, is firing 2 or 3 times but only if data was actually passed between the tags…

    <RegID>9999</RegID>     This will fire characters more than once.
   <TempID></TempID>  Fires Characters only one.

Still not sure I am approaching this as I should. I looked at using XMLDocument, but think using XMLReader looks to be much simpler to code. Perhaps I am wrong on this count?

John

[quote=202609:@John Baughman]Ok. I can see now what is happening. The parser fires several times on the same element. On one of those firings Characters gets what is between the tags on all the other hits it gets what looks like a carriage return or a line feed. The s string has 2 lines with nothing on the lines.

What’s up with that.[/quote]
It’s meant to read a file as a stream, so yes the characters event may fire more than once.

Just do this:

Output.Value(key)=output.lookup(key,"") + value