Results 1 to 9 of 9

Thread: [RESOLVED] Reading XML File

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Posts
    70

    Resolved [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 &amp; 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.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    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:
    1. Private Sub Form_Load()
    2.     Dim xmlDoc As New DOMDocument
    3.     Dim N1 As Object
    4.  
    5.    
    6.     xmlDoc.Load ("C:\test.xml")
    7.     Debug.Print xmlDoc.xml
    8.     For Each N1 In xmlDoc.getElementsByTagName("station")
    9.         Debug.Print N1.id
    10.         Debug.Print N1.getElementsByTagName("callSign").Text
    11.         Debug.Print N1.getElementsByTagName("name").Text
    12.         Debug.Print N1.getElementsByTagName("affiliate").Text
    13.     Next
    14.            
    15. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Posts
    70

    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

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    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"

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Posts
    70

    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

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Reading XML File

    yep good catch.. this should get you started

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim xmlDoc As New DOMDocument
    3.     Dim N1 As Object
    4.     Dim N2 As Object
    5.    
    6.     xmlDoc.Load ("C:\test.xml")
    7.     For Each N1 In xmlDoc.getElementsByTagName("station")
    8.         Debug.Print N1.Text
    9.         For Each N2 In N1.getElementsByTagName("callSign")
    10.             Debug.Print N2.Text
    11.         Next
    12.         For Each N2 In N1.getElementsByTagName("name")
    13.             Debug.Print N2.Text
    14.         Next
    15.         For Each N2 In N1.getElementsByTagName("affiliate")
    16.             Debug.Print N2.Text
    17.         Next
    18.     Next
    19.    
    20. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Posts
    70

    Re: Reading XML File

    Works great!, Thanks.

  8. #8
    New Member
    Join Date
    Sep 2006
    Posts
    1

    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 --

  9. #9
    Lively Member
    Join Date
    Jul 2002
    Posts
    115

    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
  •  



Click Here to Expand Forum to Full Width