|
-
Jul 21st, 2006, 09:28 AM
#1
Thread Starter
Lively Member
[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
Last edited by ImpShial; Jul 21st, 2006 at 10:19 AM.
-
Jul 21st, 2006, 09:52 AM
#2
Re: Reading XML File
add a reference to the Microsoft XML vX.0...
ok.. testing it I cant get it to work..
I did something similar and this worked... maybe its because the file isnt complete or the xsl (or whatever it is is missing?)
either way the xml load showed nothing..... odd
VB Code:
Private Sub Form_Load()
Dim xmlDoc As New DOMDocument
Dim N1 As Object
xmlDoc.Load ("C:\test.xml")
Debug.Print xmlDoc.xml
For Each N1 In xmlDoc.getElementsByTagName("station")
Debug.Print N1.id
Debug.Print N1.getElementsByTagName("callSign").Text
Debug.Print N1.getElementsByTagName("name").Text
Debug.Print N1.getElementsByTagName("affiliate").Text
Next
End Sub
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jul 21st, 2006, 10:14 AM
#3
Thread Starter
Lively Member
Re: Reading XML File
I fixed the XML. There was an extra <station> tag without closure in there.
That is odd. I messed around with your code and got the same thing. Wonder why it won't load?
Can anyone help with this issue?
Thanks
-
Jul 21st, 2006, 10:17 AM
#4
Re: Reading XML File
is there xsl file?
I cant even get it to load in IE?
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jul 21st, 2006, 10:21 AM
#5
Thread Starter
Lively Member
Re: Reading XML File
Fixed another issue in the XML file. There was a loose & in the first station name: A & E.
I added "amp;" after the & symbol and the app was able to read the xml file.
Imp
-
Jul 21st, 2006, 10:33 AM
#6
Re: Reading XML File
yep good catch.. this should get you started
VB Code:
Private Sub Form_Load()
Dim xmlDoc As New DOMDocument
Dim N1 As Object
Dim N2 As Object
xmlDoc.Load ("C:\test.xml")
For Each N1 In xmlDoc.getElementsByTagName("station")
Debug.Print N1.Text
For Each N2 In N1.getElementsByTagName("callSign")
Debug.Print N2.Text
Next
For Each N2 In N1.getElementsByTagName("name")
Debug.Print N2.Text
Next
For Each N2 In N1.getElementsByTagName("affiliate")
Debug.Print N2.Text
Next
Next
End Sub
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jul 21st, 2006, 10:55 AM
#7
Thread Starter
Lively Member
-
Sep 28th, 2006, 07:36 AM
#8
New Member
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 --
-
Dec 11th, 2006, 11:25 AM
#9
Lively Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|