Results 1 to 7 of 7

Thread: [RESOLVED] read xml data and output

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    419

    Resolved [RESOLVED] read xml data and output

    hello,

    i am currently trying to get to grips with importing data from a .xml file, i want my data to be printed out into labels.

    for the sake of it lets call them Label1 and 2. Here is my xml code:

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <First id="1">
    <Topic>First XML Document</Topic>
    <Message>Not Bad For a First Attempt</Message>
    </First>
    i want to make it so it will display the topic in Label1 and in the 2nd i want it to display the message.

    please help, the internet is hell to find something on this,
    thanks
    Lee
    If a post has been usefull then Rate it!

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: read xml data and output

    Try this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Dim tmp As String, ff As Integer: ff = FreeFile
    5.         'read it...
    6.         Open "c:\my.xml" For Input As #ff
    7.             tmp = Input(LOF(ff), ff)
    8.         Close #ff
    9.             'call the findBetween function...
    10.             Label1.Caption = findBetween(tmp, "<Topic>", "</Topic>")
    11.             Label2.Caption = findBetween(tmp, "<Message>", "</Message>")
    12. End Sub
    13.  
    14. Private Function findBetween(inString As String, fromString As String, toString As String) As String
    15.     Dim fromPos As Long, toPos As Long
    16.         fromPos = InStr(1, inString, fromString)
    17.             If fromPos Then
    18.                 toPos = InStr(1, inString, toString)
    19.                     If toPos Then
    20.                         findBetween = Mid(inString, fromPos + Len(fromString), toPos - fromPos - Len(fromString))
    21.                     End If
    22.             End If
    23. End Function

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    419

    Re: read xml data and output

    thanks very much, that worked great, could you explain a little bit what the findBetween function does?
    If a post has been usefull then Rate it!

  4. #4
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: read xml data and output

    It searched for "fromString" and "toString" Strings with the InStr() function. If it finds theme it returns the String in the middle of theme with the Mid() function.

    You can actually use this function to parse out any tag/s

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    419

    Re: read xml data and output

    ah nice, thanks for that
    If a post has been usefull then Rate it!

  6. #6
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: [RESOLVED] read xml data and output

    When dealing with XML it is ususally best to use Microsofts XML parser to do the reading for you. To run this code I set a reference to Microsoft XML, v3.0 from the list of references. pretty much any version of the parser should work with this code.

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim objDoc As DOMDocument
    3. Dim objNode As IXMLDOMNode
    4.  
    5.     'load the xml document into a document object
    6.     'You can use the LoadXML method instead to load an XML string instead of a file
    7.     Set objDoc = New DOMDocument
    8.     objDoc.async = False
    9.     objDoc.Load "C:\Documents and Settings\Mark\Desktop\sample.xml"
    10.    
    11.     'Select the node you want to read and add its text to the label
    12.     Set objNode = objDoc.selectSingleNode("First/Topic")
    13.     Label1.Caption = objNode.Text
    14.    
    15.     Set objNode = objDoc.selectSingleNode("First/Message")
    16.     Label2.Caption = objNode.Text
    17.    
    18.     'do your cleanup
    19.     Set objNode = Nothing
    20.     Set objDoc = Nothing
    21. End Sub

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    419

    Re: [RESOLVED] read xml data and output

    yeah thanks, that will help me more in the future knowing how to read the child's of more than one adult tags.

    please take a look at my other post on writing to a xml file, i need help on that if you know how?
    If a post has been usefull then Rate it!

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