Results 1 to 4 of 4

Thread: XML Elements and Attributes - How to retrive?

  1. #1

    Thread Starter
    Hyperactive Member gmatteson's Avatar
    Join Date
    Feb 2002
    Location
    Rhode Island, USA
    Posts
    293

    XML Elements and Attributes - How to retrive?

    <?xml version="1.0" encoding="utf-8"?>
    <chimaera>
    <servers>
    <host ip="172.16.1.30">Chimaera</host>
    <host ip="172.16.1.31">Isis</host>
    <host ip="172.16.1.32">MySQL</host>
    <host ip="172.16.1.33">Ares</host>
    <host ip="172.16.1.34">Osiris</host>
    <host ip="172.16.1.35">Mars</host>
    <host ip="192.16.3.5">James Server</host>
    </servers>
    <themes value="true" />
    <locationupdate path="C:\1.xml" />
    </chimaera>


    this is my xml file...

    how do i loop through it ONCE and retrive the element names and attributes?

    such as...

    dim mynode as xmlnode
    dim myel as xmlelement

    for each mynode in xmldoc.documentelement
    select case mynode.childnode.name
    case "host"
    myel.value
    case "themes"
    myel.value
    case "locationupdate"
    myel.value
    next

    rather than...

    For Each xmlNode In xmlDoc.SelectNodes("//chimaera/servers/host")
    lvwItem.SubItems.Add(xmlNode.Attributes.ItemOf("ip").Value)
    Next

    and doing three of these loops to get the attributes of ip, value, and path.... any code would be appreciative thank you.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Huh? Where are the three loops? Your trouble is that you are storing info in a heirarchy xml structure so a standard loop isn't going to get the children. If you want to straight loop then why store it like that? Otherwise why not just get the three unrelated parts seperately, you shouldn't have to loop for the value and path if there is just one. Maybe explain more of what you are trying to do.

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    After re-reading your post a couple times I think understand what you are after, although my previous questions still apply. The once loop bit can be done like this:
    VB Code:
    1. Dim xdoc As New XmlDocument
    2.         xdoc.Load("..\data.xml")
    3.         For Each nod As XmlNode In xdoc.SelectNodes("//*")
    4.             Select Case nod.Name
    5.                 Case "host"
    6.                     MsgBox(nod.Attributes("ip").Value, , "host")
    7.                 Case "themes"
    8.                     MsgBox(nod.Attributes("value").Value, , "themes")
    9.                 Case "locationupdate"
    10.                     MsgBox(nod.Attributes("path").Value, , "locationupdate")
    11.             End Select
    12.         Next
    Although then you have to loop through every node even though you don't use all of them. Now in your sample there isn't much you don't use so its no biggie but if the file was larger then this would be wasteful.

    I would suggest you do it like the following:
    VB Code:
    1. Dim xdoc As New XmlDocument
    2.         xdoc.Load("..\data.xml")
    3.         For Each nod As XmlNode In xdoc.SelectNodes("//servers/host")
    4.             MsgBox(nod.Attributes("ip").Value, , "host")
    5.         Next
    6.         MsgBox(xdoc.SelectSingleNode("//themes/@value").Value, , "themes")
    7.         MsgBox(xdoc.SelectSingleNode("//locationupdate/@path").Value, , "locationupdate")

    Also if you only need the ip and not the value of the host node then you can use this in the loop instead:
    VB Code:
    1. For Each nod As XmlNode In xdoc.SelectNodes("//servers/host/@ip")
    2.             MsgBox(nod.Value, , "host")
    3.         Next

  4. #4

    Thread Starter
    Hyperactive Member gmatteson's Avatar
    Join Date
    Feb 2002
    Location
    Rhode Island, USA
    Posts
    293

    thanks

    thanks edneesis, awesome!

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