Results 1 to 6 of 6

Thread: read xml

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    138

    Unhappy read xml

    hi gurus

    New to xml so go easy on me. I know that for some of you this is no problem.

    I have an xml file:

    <root>
    <social>xxx-xx-xxxx</social>
    <color>black and white</color>
    <name>
    <first> john</first>
    <Last> smith</Last>
    </name>
    </root>

    I need to read the xml file and browse through all the nodes so I can get values on specific rules

    Need your help thanks

    so far I have the following:

    Code:
    Dim nodelist As MSXML.IXMLDOMNodeList
    Dim rootele As MSXML.IXMLDOMElement
    Dim xNode As MSXML.IXMLDOMNode
    Set xDoc = New MSXML.DOMDocument
    
    If xDoc.Load("C:\SHC_2005_07_28_16_10_59_165_SINGLE.xml") Then
          MsgBox "loaded"
       Set rootele = xDoc.documentElement
       
      Set nodelist = rootele.childNodes
         
       For Each xNode In nodelist
        'do something
       next xNode
    end if
    hoW do I browse through the child to get values.

    the code above gives me the childs of the root (social,color,name) but it does not bring up the child of name (first,last)

    Thanks again in advance

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: read xml

    I think you need to enumerate through the children of each node you are enumerating

    VB Code:
    1. Dim nodelist2 As IXMLDOMNodeList
    2. For Each xNode In nodeList
    3.     Set nodelist2 = xNode.childNodes
    4.     For Each xNode In nodelist2
    5.          ' etc...

    Off the top of my head right now I can't think of a better way, but I'm sure there is one...

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: read xml

    Gaaaah.... you would tax my brain like this....
    OK, this is all from the memory banks, so I cannot guarantee the accuracy of it 100% but it should be the 99% solution to get you going (if nothing else an idea of what to search on.)

    Ok, here's the skinny.
    THis right here:
    Set nodelist = rootele.childNodes

    That gets the children of the root node tag. Currently that is: social, color, and name. It does NOT include first or Last because they are children of the name node.

    OK, so your loop is good for the first level, but we want to dig deeper. Every node has a property called .hasChild (it might be .hasChildren - in either case it's a boolean). You can't check .childNodes.Length, because if it doesn't have children, it will be NULL and generates all kind of errors about objects not instaciated, blah, blah, blah.

    Ok, so once you determine that the current node you are looking at hasChildren then you can set another nodelist variable to those children and iterate through them as well. If you are only needing to go two levels deep, this should be OK. If you plan on going any further, you may want to consider either a recursive function that takes the childNodes as a parameter, or possibly learn a little about the .selectNodes/.selectSingleNode functions. They are a bit more complex, but very powerful in being able to query out specific nodes that fit criteria (it's kinda like an XML version of SQL).

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    138

    Re: read xml

    Thanks guys, thanks a lot, I will check into it

    Thanks again

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    138

    Re: read xml

    tg

    Any code that I can use as sample. on recursive funtion or selectnode,selectsinglenode

    or any link that can show me how?
    thanks

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: read xml

    Let me get back to you..... I know I used to... but that was beforethe pc melt down of May05... I'll see if I can dig something up.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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