XMLDocument
From Xojo Documentation
Class (inherits from XMLNode)
Use to represents an XML document.
Properties | |||||||||||||||||
|
Constructors | |||
|
Notes
The XML parser is based on Expat 1.95.7.
An XMLDocument is an internal data structure that can be converted to a string using the ToString function. An XMLDocument is used for both parsing existing XML data into a DOM document structure or creating a new XML document from scratch.
Examples
The following XML:
<?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>
Can be created using this code, which displays the XML to a TextArea and prompts you to save it to a file:
Var xml As New XmlDocument
Var root As XmlNode
root = xml.AppendChild(xml.CreateElement("League"))
Var teamNode As XmlNode
Var playerNode As XmlNode
// Create 1st team and its players
teamNode = root.AppendChild(xml.CreateElement("Team"))
teamNode.SetAttribute("name", "Seagulls")
playerNode = teamNode.AppendChild(xml.CreateElement("Player"))
playerNode.SetAttribute("name", "Bob")
playerNode.SetAttribute("position", "1B")
playerNode = teamNode.AppendChild(xml.CreateElement("Player"))
playerNode.SetAttribute("name", "Tom")
playerNode.SetAttribute("position", "2B")
// Create 2nd team and its players
teamNode = root.AppendChild(xml.CreateElement("Team"))
teamNode.SetAttribute("name", "Pigeons")
playerNode = teamNode.AppendChild(xml.CreateElement("Player"))
playerNode.SetAttribute("name", "Bill")
playerNode.SetAttribute("position", "1B")
playerNode = teamNode.AppendChild(xml.CreateElement("Player"))
playerNode.SetAttribute("name", "Tim")
playerNode.SetAttribute("position", "2B")
// Create 3rd team and its players
teamNode = root.AppendChild(xml.CreateElement("Team"))
teamNode.SetAttribute("name", "Crows")
playerNode = teamNode.AppendChild(xml.CreateElement("Player"))
playerNode.SetAttribute("name", "Ben")
playerNode.SetAttribute("position", "1B")
playerNode = teamNode.AppendChild(xml.CreateElement("Player"))
playerNode.SetAttribute("name", "Ty")
playerNode.SetAttribute("position", "2B")
TextArea1.Value = xml.ToString
DisplayXML(xml)
Var root As XmlNode
root = xml.AppendChild(xml.CreateElement("League"))
Var teamNode As XmlNode
Var playerNode As XmlNode
// Create 1st team and its players
teamNode = root.AppendChild(xml.CreateElement("Team"))
teamNode.SetAttribute("name", "Seagulls")
playerNode = teamNode.AppendChild(xml.CreateElement("Player"))
playerNode.SetAttribute("name", "Bob")
playerNode.SetAttribute("position", "1B")
playerNode = teamNode.AppendChild(xml.CreateElement("Player"))
playerNode.SetAttribute("name", "Tom")
playerNode.SetAttribute("position", "2B")
// Create 2nd team and its players
teamNode = root.AppendChild(xml.CreateElement("Team"))
teamNode.SetAttribute("name", "Pigeons")
playerNode = teamNode.AppendChild(xml.CreateElement("Player"))
playerNode.SetAttribute("name", "Bill")
playerNode.SetAttribute("position", "1B")
playerNode = teamNode.AppendChild(xml.CreateElement("Player"))
playerNode.SetAttribute("name", "Tim")
playerNode.SetAttribute("position", "2B")
// Create 3rd team and its players
teamNode = root.AppendChild(xml.CreateElement("Team"))
teamNode.SetAttribute("name", "Crows")
playerNode = teamNode.AppendChild(xml.CreateElement("Player"))
playerNode.SetAttribute("name", "Ben")
playerNode.SetAttribute("position", "1B")
playerNode = teamNode.AppendChild(xml.CreateElement("Player"))
playerNode.SetAttribute("name", "Ty")
playerNode.SetAttribute("position", "2B")
TextArea1.Value = xml.ToString
DisplayXML(xml)
This code iterates through the XML created above and displays it in a ListBox:
Sub DisplayXML(xml As XmlDocument)
Var root As XmlNode
root = xml.DocumentElement
XMLList.DeleteAllRows
Var teamNode As XmlNode
Var playerNode As XmlNode
For team As Integer = 0 To root.ChildCount - 1
// Add Team name
teamNode = root.Child(team)
XMLList.AddRow(teamNode.GetAttribute("name"))
// Add Players
For player As Integer = 0 To teamNode.ChildCount - 1
playerNode = teamNode.Child(player)
XMLList.AddRow(playerNode.GetAttribute("name"), _
playerNode.GetAttribute("position"))
Next
Next
End Sub
Var root As XmlNode
root = xml.DocumentElement
XMLList.DeleteAllRows
Var teamNode As XmlNode
Var playerNode As XmlNode
For team As Integer = 0 To root.ChildCount - 1
// Add Team name
teamNode = root.Child(team)
XMLList.AddRow(teamNode.GetAttribute("name"))
// Add Players
For player As Integer = 0 To teamNode.ChildCount - 1
playerNode = teamNode.Child(player)
XMLList.AddRow(playerNode.GetAttribute("name"), _
playerNode.GetAttribute("position"))
Next
Next
End Sub
See Also
XMLAttribute, XMLComment, XMLCDATASection, XMLDOMException, XMLElement, XMLException, XMLNode, XMLNodeList, XMLProcessingInstruction, XMLTextNode classes.