Results 1 to 3 of 3

Thread: [RESOLVED] Need Help in XML and vb to retrieve values

  1. #1

    Thread Starter
    Hyperactive Member csKanna's Avatar
    Join Date
    Dec 2005
    Location
    Tech-Tips-Now.com
    Posts
    339

    Resolved [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.

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    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:
    1. Option Explicit
    2.  
    3. Private Sub cmdGo_Click()
    4.   Dim strXml As String
    5.   Dim oXmlDoc As Object
    6.   Dim oXmlNodes As Object
    7.   Dim oXmlNode As Object
    8.  
    9.   ' Get the example Xml
    10.   strXml = CreateExampleXml()
    11.  
    12.   ' Create an XML Document object
    13.   Set oXmlDoc = CreateObject("MSXML2.DOMDocument")
    14.   ' Load the Xml string into the Document
    15.   Call oXmlDoc.loadXML(strXml)
    16.   ' USe the "SelectNodes" method and an XPath query
    17.   ' to get all the "Item" nodes in the XML document
    18.   Set oXmlNodes = _
    19.     oXmlDoc.DocumentElement.selectNodes("/log/Fruits/Stock/item")
    20.    
    21.   ' Enumerate the gathered "Item" nodes
    22.   For Each oXmlNode In oXmlNodes
    23.     ' Output the "Item" node's text
    24.     Debug.Print oXmlNode.Text
    25.   Next
    26. End Sub
    27.  
    28. ' Generates some example XML and returns it as a string
    29. Private Function CreateExampleXml() As String
    30.   Dim strXml As String
    31.  
    32.   strXml = strXml & "<log>"
    33.   strXml = strXml & "  <Fruits>"
    34.   strXml = strXml & "    <date>18-Apr-2005</date>"
    35.   strXml = strXml & "    <time>16:14:22</time>"
    36.   strXml = strXml & "    <Stock>"
    37.   strXml = strXml & "      <item>Banana</item>"
    38.   strXml = strXml & "      <item>Apple</item>"
    39.   strXml = strXml & "      <item>Grapes</item>"
    40.   strXml = strXml & "      <item>Mango</item>"
    41.   strXml = strXml & "      <item>Others</item>"
    42.   strXml = strXml & "    </Stock>"
    43.   strXml = strXml & "  </Fruits>"
    44.   strXml = strXml & "</log>"
    45.  
    46.   CreateExampleXml = strXml
    47. End Function
    Regards,

    - Aaron.

  3. #3

    Thread Starter
    Hyperactive Member csKanna's Avatar
    Join Date
    Dec 2005
    Location
    Tech-Tips-Now.com
    Posts
    339

    Re: Need Help in XML and vb to retrieve values

    thanks a lot works great

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