Agent Smith (aka Jason S) had me looking at what it would take to consume some XML files in VB6. Thankfully I had been doing work with VB recently and so had some clue's about what to do.
It should be noted that I was loading the XML into DOM, which actually puts the whole thing into memory so while it is flexible (forward and backwards reads) it will potentially consume a lot of memory.
If we consider this XML snippet.
<LastName>Agent</LastName>
<FirstName>Smith</FirstName>
<Email><a href="mailto:asmith@matrix.com">asmith@matrix.com</a> </Email>
<?xml version='1.0'?>
<MyInfo>
<LastName>Agent</LastName>
<FirstName>Smith</FirstName>
<Email><a href="mailto:asmith@matrix.com">asmith@matrix.com</a> </Email>
</MyInfo>
<?xml version='1.0'?>
<MyInfo>
<LastName>Agent</LastName>
<FirstName>Smith</FirstName>
<Email><a href="mailto:asmith@matrix.com">asmith@matrix.com</a> </Email>
</MyInfo>
Then we can access it like this...
Dim Xml As New MSXML2.FreeThreadedDOMDocument
Dim Root As MSXML2.IXMLDOMNode
Dim Agents As MSXML2.IXMLDOMNode
Dim Agent As MSXML2.IXMLDOMNode
'Load the XML string to the DOM document
If Xml.Load(Left("C\MyFile.xml") Then
Set Root = Xml.documentElement
Set Agents = Root.selectSingleNode("/MyInfo")
sLastName = Agents.selectSingleNode("LastName").Text
sFirstName = Agents.selectSingleNode("FirstName").Text
sEmail = Agents.selectSingleNode("Email").Text
Dim Xml As New MSXML2.FreeThreadedDOMDocument
Dim Root As MSXML2.IXMLDOMNode
Dim Agents As MSXML2.IXMLDOMNode
Dim Agent As MSXML2.IXMLDOMNode
Dim sLastName as String
Dim sFirstName as String
Dim sEmail as String
'Load the XML string to the DOM document
Xml.async = False
If Xml.Load(Left("C\MyFile.xml") Then
Set Root = Xml.documentElement
Set Agents = Root.selectSingleNode("/MyInfo")
sLastName = Agents.selectSingleNode("LastName").Text
sFirstName = Agents.selectSingleNode("FirstName").Text
sEmail = Agents.selectSingleNode("Email").Text
End If
Dim Xml As New MSXML2.FreeThreadedDOMDocument
Dim Root As MSXML2.IXMLDOMNode
Dim Agents As MSXML2.IXMLDOMNode
Dim Agent As MSXML2.IXMLDOMNode
Dim sLastName as String
Dim sFirstName as String
Dim sEmail as String
'Load the XML string to the DOM document
Xml.async = False
If Xml.Load(Left("C\MyFile.xml") Then
Set Root = Xml.documentElement
Set Agents = Root.selectSingleNode("/MyInfo")
sLastName = Agents.selectSingleNode("LastName").Text
sFirstName = Agents.selectSingleNode("FirstName").Text
sEmail = Agents.selectSingleNode("Email").Text
End If
or if there are multiple agents as below looping could be done like this...
<LastName>Smith</LastName>
<FirstName>Agent</FirstName>
<Email>asmith@matrix.com</Email>
<LastName>OtherGuy</LastName>
<FirstName>WithGlasses</FirstName>
<Email>wg@matrix.com</Email>
<LastName>Last</LastName>
<FirstName>Agent</FirstName>
<Email>la@matrix.com</Email>
<?xml version='1.0'?>
<MyInfo>
<MatrixAgents>
<Agent>
<LastName>Smith</LastName>
<FirstName>Agent</FirstName>
<Email>asmith@matrix.com</Email>
</Agent>
<Agent>
<LastName>OtherGuy</LastName>
<FirstName>WithGlasses</FirstName>
<Email>wg@matrix.com</Email>
</Agent>
<Agent>
<LastName>Last</LastName>
<FirstName>Agent</FirstName>
<Email>la@matrix.com</Email>
</Agent>
</MatrixAgents>
</MyInfo>
<?xml version='1.0'?>
<MyInfo>
<MatrixAgents>
<Agent>
<LastName>Smith</LastName>
<FirstName>Agent</FirstName>
<Email>asmith@matrix.com</Email>
</Agent>
<Agent>
<LastName>OtherGuy</LastName>
<FirstName>WithGlasses</FirstName>
<Email>wg@matrix.com</Email>
</Agent>
<Agent>
<LastName>Last</LastName>
<FirstName>Agent</FirstName>
<Email>la@matrix.com</Email>
</Agent>
</MatrixAgents>
</MyInfo>
If Xml.Load(Left("C:\MyFile.xml") Then
Set Root = Xml.documentElement
Set Agents = Root.selectSingleNode ("/MyInfo/MatrixAgents")
For Each Agent In Agents.childNodes
if Agent.selectSingleNode("LastName").Text = "Smith" THen
sLastName = Agent.selectSingleNode ("LastName").Text
sFirstName = Agent.selectSingleNode("FirstName").Text
sEmail = Agent.selectSingleNode("Email").Text
Xml.async = False
If Xml.Load(Left("C:\MyFile.xml") Then
Set Root = Xml.documentElement
Set Agents = Root.selectSingleNode ("/MyInfo/MatrixAgents")
For Each Agent In Agents.childNodes
'Get Agent Smith
if Agent.selectSingleNode("LastName").Text = "Smith" THen
sLastName = Agent.selectSingleNode ("LastName").Text
sFirstName = Agent.selectSingleNode("FirstName").Text
sEmail = Agent.selectSingleNode("Email").Text
End if
Next
GoTo Done
End If
Xml.async = False
If Xml.Load(Left("C:\MyFile.xml") Then
Set Root = Xml.documentElement
Set Agents = Root.selectSingleNode ("/MyInfo/MatrixAgents")
For Each Agent In Agents.childNodes
'Get Agent Smith
if Agent.selectSingleNode("LastName").Text = "Smith" THen
sLastName = Agent.selectSingleNode ("LastName").Text
sFirstName = Agent.selectSingleNode("FirstName").Text
sEmail = Agent.selectSingleNode("Email").Text
End if
Next
GoTo Done
End If
Remember to clean up after yourself with vb6 ... Xml = Nothing ... etc
"He who praises everybody, praises nobody." - Samuel Johnson
Technorati tags:
Visual Basic 6,
XML,
DOM
Comments are closed.