|
-
Dec 19th, 2006, 11:04 AM
#1
Thread Starter
Hyperactive Member
[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! 
-
Dec 19th, 2006, 11:18 AM
#2
Re: read xml data and output
Try this:
VB Code:
Option Explicit
Private Sub Command1_Click()
Dim tmp As String, ff As Integer: ff = FreeFile
'read it...
Open "c:\my.xml" For Input As #ff
tmp = Input(LOF(ff), ff)
Close #ff
'call the findBetween function...
Label1.Caption = findBetween(tmp, "<Topic>", "</Topic>")
Label2.Caption = findBetween(tmp, "<Message>", "</Message>")
End Sub
Private Function findBetween(inString As String, fromString As String, toString As String) As String
Dim fromPos As Long, toPos As Long
fromPos = InStr(1, inString, fromString)
If fromPos Then
toPos = InStr(1, inString, toString)
If toPos Then
findBetween = Mid(inString, fromPos + Len(fromString), toPos - fromPos - Len(fromString))
End If
End If
End Function
-
Dec 19th, 2006, 11:25 AM
#3
Thread Starter
Hyperactive Member
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! 
-
Dec 19th, 2006, 11:27 AM
#4
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
-
Dec 19th, 2006, 11:33 AM
#5
Thread Starter
Hyperactive Member
Re: read xml data and output
ah nice, thanks for that
If a post has been usefull then Rate it! 
-
Dec 19th, 2006, 01:46 PM
#6
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:
Private Sub Command1_Click()
Dim objDoc As DOMDocument
Dim objNode As IXMLDOMNode
'load the xml document into a document object
'You can use the LoadXML method instead to load an XML string instead of a file
Set objDoc = New DOMDocument
objDoc.async = False
objDoc.Load "C:\Documents and Settings\Mark\Desktop\sample.xml"
'Select the node you want to read and add its text to the label
Set objNode = objDoc.selectSingleNode("First/Topic")
Label1.Caption = objNode.Text
Set objNode = objDoc.selectSingleNode("First/Message")
Label2.Caption = objNode.Text
'do your cleanup
Set objNode = Nothing
Set objDoc = Nothing
End Sub
-
Dec 19th, 2006, 01:51 PM
#7
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|