[RESOLVED] Reading XML File
I'm fairly new at the whole XML thing, but I am trying to write some code that will give me data from an XML file.
Here's my issue. I have a file with multiple levels, and the data I want is 7-8 levels deep.
In the following XML file, I would like to return each <station> node, it's id attribute, and all of it's child-node data.
Code:
<?xml version="1.0" encoding="utf-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:downloadResponse SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="urn:TMSWebServices">
<xtvdResponse xsi:type="ns1:xtvdResponse">
<messages xsi:type="ns1:messages">
<message>Your subscription will expire: 2006-10-19T22:26:10Z</message>
</messages>
<xtvd from="2006-07-19T05:00:00Z" to="2006-07-26T05:00:00Z" schemaVersion="1.3" xmlns="urn:TMSWebServices" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:TMSWebServices http://docs.tms.tribune.com/tech/xml/schemas/tmsxtvd.xsd">
<stations>
<station id="10035">
<callSign>AETV</callSign>
<name>A & E Network</name>
<affiliate>Satellite</affiliate>
</station>
<station id="10021">
<callSign>AMC</callSign>
<name>AMC</name>
<affiliate>Satellite</affiliate>
</station>
<station id="16331">
<callSign>ANIMAL</callSign>
<name>Animal Planet</name>
<affiliate>Satellite</affiliate>
</station>
<station id="10284">
<callSign>AZNTV</callSign>
<name>AZNTV</name>
<affiliate>Satellite</affiliate>
</station>
<station id="18332">
<callSign>BBCA</callSign>
<name>BBC America</name>
<affiliate>Satellite</affiliate>
</station>
<station id="10051">
<callSign>BET</callSign>
<name>Black Entertainment Television</name>
<affiliate>Satellite</affiliate>
</station>
</stations>
</xtvd>
</xtvdResponse>
</ns1:downloadResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
This file is a small part of a larger file (the Zap2It TV Listings Download), but I'm using this chunk to get some help.
I would like to use SelectNodes with XPath query language, but I'm having some trouble wrapping my head around it.
Any help would be much appreciated with an example specific to the XML I posted above.
Thank You,
Imp
Re: [RESOLVED] Reading XML File
Yeah, I know it says "Resolved," and it was... but it isn't! The original request was to use SelectNodes and XPath. The answer was using the DOM Object methods. Which works for this situation, but what happens if you have multiple namespaces? And, where's the SelectNodes in the "answer?"
I've spent almost a week with the same question. There's, like, NOTHING online that's helpful. But, I found something that works, and it seems to actually answer the question. Here it is, using the example data supplied in the original request...
Code:
Option Explicit
Sub Main()
Dim sDataPath As String
sDataPath = "C:\_tmp\"
Dim sXmlDocRadio As String
sXmlDocRadio = "stations.xml"
Dim oXmlDoc As New DOMDocument30
Dim oStationsAxes As IXMLDOMNodeList ' a Collection of Station entries
Dim oStationNode As IXMLDOMNode ' a single station
oXmlDoc.async = False
oXmlDoc.validateOnParse = False
oXmlDoc.resolveExternals = False
oXmlDoc.preserveWhiteSpace = True
oXmlDoc.setProperty "SelectionLanguage", _
"XPath"
oXmlDoc.setProperty "SelectionNamespaces", _
"xmlns:TMSWebServices='urn:TMSWebServices'"
If oXmlDoc.Load(sDataPath & sXmlDocRadio) = False Then
MsgBox "Failed to load xml data from file."
Exit Sub
End If
' E.G. we want all the stations, using XPath:
Set oStationsAxes = _
oXmlDoc.selectNodes("//TMSWebServices:stations/TMSWebServices:station")
' You could then iterate through these, but we'll skip it for now!
' It's just an example you can play with later.
' When you look at this obj in your locals window, look under children,
' and then item(n).ChildNodes.item(evenNumbers). I'll stop now!
For Each oStationNode In _
oXmlDoc.selectNodes("//TMSWebServices:stations/TMSWebServices:station")
Debug.Print "Station ID: " _
& oStationNode.selectSingleNode("@id").Text
Debug.Print vbTab & "Callsign: " _
& oStationNode.selectSingleNode("TMSWebServices:callSign").Text
Debug.Print vbTab & "Name: " _
& oStationNode.selectSingleNode("TMSWebServices:name").Text
Debug.Print vbTab & "Affiliate: " _
& oStationNode.selectSingleNode("TMSWebServices:affiliate").Text _
& vbCrLf
Next oStationNode
Set oStationNode = Nothing
Set oStationsAxes = Nothing
Set oXmlDoc = Nothing
End Sub
In my work, I use TestPartner from Compuware to test applications. Test data sometimes comes from XML sources. I obviously need to find SOME way of processing the data. Most examples you can find online these days uses This.NET and That.NET, but VBA isn't .NET in TestPartner, and VB6 examples are getting hard to find! And the ones you CAN find, use very simple XML documents.
Enjoy! And, sorry for the intrusion!
-- c0nfuted --
Re: [RESOLVED] Reading XML File
Hi
I did try youe sample how to read XML
I use it with listview but I can not use letter like " ð "
how can I fix this
Regards
Sig