|
-
Apr 21st, 2006, 11:21 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Need Help in XML and vb to retrieve values
Hi all,
I have XML file as shown below:
Code:
<log>
<Fruits>
<date>18-Apr-2005</date>
<time>16:14:22<time>
<Stock>
<item>Banana</item>
<item>Apple</item>
<item>Grapes</item>
<item>Mango</item>
<item>Others</item>
</Stock>
</Fruits>
</log>
I need output like:
Banana
Apple
Grapes
Mango
Others
i.e., I want to retrieve the value in between <item>, </item>.
I searched the forum but haven't find anything like my request.
Please help me on this.
Thanks in advance.
Kanna.
-
Apr 22nd, 2006, 02:07 AM
#2
Re: Need Help in XML and vb to retrieve values
You can use the Micrsoft XML DOM to manipulate XML documents very easily,
here's an example...
note: I have used latebinding here for the sake of making the example simple to run,
you would likely find it easier to use the DOM if you first add a reference to
the Microsoft XML library and use typed objects to get intellisense.
VB Code:
Option Explicit
Private Sub cmdGo_Click()
Dim strXml As String
Dim oXmlDoc As Object
Dim oXmlNodes As Object
Dim oXmlNode As Object
' Get the example Xml
strXml = CreateExampleXml()
' Create an XML Document object
Set oXmlDoc = CreateObject("MSXML2.DOMDocument")
' Load the Xml string into the Document
Call oXmlDoc.loadXML(strXml)
' USe the "SelectNodes" method and an XPath query
' to get all the "Item" nodes in the XML document
Set oXmlNodes = _
oXmlDoc.DocumentElement.selectNodes("/log/Fruits/Stock/item")
' Enumerate the gathered "Item" nodes
For Each oXmlNode In oXmlNodes
' Output the "Item" node's text
Debug.Print oXmlNode.Text
Next
End Sub
' Generates some example XML and returns it as a string
Private Function CreateExampleXml() As String
Dim strXml As String
strXml = strXml & "<log>"
strXml = strXml & " <Fruits>"
strXml = strXml & " <date>18-Apr-2005</date>"
strXml = strXml & " <time>16:14:22</time>"
strXml = strXml & " <Stock>"
strXml = strXml & " <item>Banana</item>"
strXml = strXml & " <item>Apple</item>"
strXml = strXml & " <item>Grapes</item>"
strXml = strXml & " <item>Mango</item>"
strXml = strXml & " <item>Others</item>"
strXml = strXml & " </Stock>"
strXml = strXml & " </Fruits>"
strXml = strXml & "</log>"
CreateExampleXml = strXml
End Function
Regards,
- Aaron.
-
May 4th, 2006, 02:40 AM
#3
Thread Starter
Hyperactive Member
Re: Need Help in XML and vb to retrieve values
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
|