Results 1 to 7 of 7

Thread: Need a hand with XML, please [RSLVD]

  1. #1

    Thread Starter
    Fanatic Member invitro's Avatar
    Join Date
    Jan 2000
    Location
    Outside your window
    Posts
    547

    Need a hand with XML, please [RSLVD]

    Allright this is an easy one for all who have worked much with XML. I understand the structure, I think.. but I'm having issues with the syntax. I tried sniffing around this forum with no luck. The examples don't have what I'm looking for, are too complex, or I am generaly blind.

    I have a file such as this one called Commands.xml

    Code:
    <FUNCTIONS>
    <COMMAND>
     <WHOIS>
      <NAME Tag="Max">That is me</NAME> 
     </WHOIS>
    </COMMAND>
    All I want to do, is create a domDocument (done) and read the first element.. which is COMMAND. After that, I want to cycle through all the children of COMMAND incase there are more NAME elements.

    My code:
    Code:
    'This is a type I have created
    Private Type xmlCommandTree
    
        docFile As New DOMDocument30
        docParent As IXMLDOMElement
        docCommand As IXMLDOMElement
        docName As IXMLDOMElement
        
    
    End Type
    ------------------------------------------
    'Actuall function
    
       Dim xCommand As xmlCommandTree
        With xCommand
        
            .docFile.Load "\commands.xml"
            Set .docCommand = .docFile.firstChild
            Set .docParent = .docCommand.firstChild
    
            'Here I would put in a loop that cycles through all the children
            'However, when I run this piece of code it sais:
            'Object variable or With Block not set - Whats that mean?
    
      End With

    Can someone give me a hand before all my hair turns gray.
    Thanks
    Last edited by invitro; May 30th, 2004 at 02:29 PM.
    ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet?

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    I don't understand what you are doing with the UDT but you can do it this way.

    VB Code:
    1. Dim docFile As New DOMDocument30
    2. Dim docParent As IXMLDOMNode
    3. Dim docName As IXMLDOMNodeList
    4. Dim intIndex As Integer
    5.  
    6. docFile.Load "C:\temp\test.xml"
    7. Set docParent = docFile.documentElement.selectSingleNode("COMMAND")
    8. Set docName = docParent.selectNodes("./WHOIS/NAME") ' The ./ says look starting at the current node
    9.  
    10. For intIndex = 0 To docName.length
    11.     Debug.Print docName(intIndex).Text
    12. Next
    13. Set docParent = Nothing
    14. Set docName = Nothing

  3. #3
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    NJ, USA
    Posts
    326
    I have a program that manipulates a large XML file but it doesn't really care about what is in the file other than this snippet of code:

    VB Code:
    1. Set root = xmlDoc.selectSingleNode("/ZoneLabsSettings/ruleset/zones/restricted")
    2. Set oNodeList = root.selectNodes("*")
    3.  
    4. For Index = 0 To (oNodeList.length - 1)
    5.  
    6.     Set NewNode = oNodeList.nextNode
    7.     If InStr(NewNode.getAttribute("description"), Chr(232) & Chr(255)) Then
    8.         root.removeChild NewNode
    9.     End If
    10. Next Index

    It searches through the children of a node and deletes the ones that match with a certain two characters in their string.
    VB.NET 2005 Express with .Net 2.0
    C# 2010 .Net 4.0

  4. #4

    Thread Starter
    Fanatic Member invitro's Avatar
    Join Date
    Jan 2000
    Location
    Outside your window
    Posts
    547
    the UDT is just straight outa my code. I put it there to make things easier.. but your code is totaly valid, and easier to read as well.

    So what I dont understand then, is the NODE part.
    I thought anything inside of <> was an element. Therefore, a statement like:

    <test>
    <test2>
    </test2>
    <test>

    would be an ELEMENT inside of an ELEMENT. Therefore, I thought you could simply GET the ELEMNT of an ELEMNT. Could you please explain why use a node, and why my code woulden't work?

    Thanks for the replies guys, great work.
    ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet?

  5. #5

  6. #6

    Thread Starter
    Fanatic Member invitro's Avatar
    Join Date
    Jan 2000
    Location
    Outside your window
    Posts
    547
    I see... wonder why my code block does not work?
    O well, I will go try your method. Thanks again!
    ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet?

  7. #7

    Thread Starter
    Fanatic Member invitro's Avatar
    Join Date
    Jan 2000
    Location
    Outside your window
    Posts
    547
    Ok I feel really dumb. My first example did infact work, however.... the filepath was pointing at the wrong file so it was opening a blank document. So it gave me the same error when i tried your method. Sheesh, now i feel dumb.

    Ooops, thanks a lot guys.
    ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet?

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