XMLNode.XQL
From Xojo Documentation
Method
XMLNode.XQL(Query as String [,Map() as String]) As XMLNodeList
Supported for all project types and targets.
Supported for all project types and targets.
Performs an XPath 1.0 query and returns an XMLNodeList of the resulting nodes.
Notes
If the query has namespace references in it, you must provide declarations for them in the form of a string array, such as:
Var map() As String = Array("ns1","http://foo","ns2","http://bar")
Xql is also called XPath. Version 1.0 of XPath are supported.
For more information about XPath:
Example
The example code below uses this XML. Assign it to a constant called kTestXML:
<?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="Who" position="1B" /> <Player name="What" position="2B" /> <Player name="I Don't Know" position="3B" /> </Team> </League>
To get all the player names, you can use "//Player" as the query:
// Load XML
Var xml As New XmlDocument
Try
xml.LoadXml(kTestXml)
Catch e As XmlException
MessageBox("XML error: " + e.Message)
End Try
// Display all the Player names
Var nodes As XmlNodeList
nodes = xml.XQL("//Player") // Find all Player nodes in XML
// Loop through results and display each name attribute
Var node As XmlNode
For i As Integer = 0 To nodes.Length - 1
node = nodes.Item(i)
MessageBox("Player: " + node.GetAttribute("name"))
Next
Var xml As New XmlDocument
Try
xml.LoadXml(kTestXml)
Catch e As XmlException
MessageBox("XML error: " + e.Message)
End Try
// Display all the Player names
Var nodes As XmlNodeList
nodes = xml.XQL("//Player") // Find all Player nodes in XML
// Loop through results and display each name attribute
Var node As XmlNode
For i As Integer = 0 To nodes.Length - 1
node = nodes.Item(i)
MessageBox("Player: " + node.GetAttribute("name"))
Next
To get just the players on the Pigeons, you can use this as the XQL query:
//Team[@name='Pigeons']/Player