Results 1 to 2 of 2

Thread: xml and xpath

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Location
    Europe, Lithuania
    Posts
    309

    Exclamation xml and xpath

    Hello,

    I'm trying to understan how DOM works.

    I have xml etc.:

    Code:
    <root>
      <file>
        <Filename>file1.txt</Filename>
        <Atributes>
          <Atribute>ReadOnly</Atribute>
          <Atribute>Hidden</Atribute>
        </Atributes> 
      </file>
      <file>
        <Filename>file2.txt</Filename>
        <Atributes>
          <Atribute>Hidden</Atribute>
        </Atributes> 
      </file>
    </root>
    To select file "file2.txt" I use this code:

    VB Code:
    1. Dim oXMLDoc As MSXML2.DOMDocument40
    2. Dim oXMLNode As MSXML2.IXMLDOMNode
    3. Dim oXMLNodeList As MSXML2.IXMLDOMNodeList
    4. Set oXMLDoc = New MSXML2.DOMDocument40
    5.  
    6. oXMLDoc.async = False
    7. oXMLDoc.resolveExternals = True
    8.  
    9. oXMLDoc.Load ("C:\files.xml")
    10. oXMLDoc.setProperty "SelectionLanguage", "XPath"
    11.  
    12. Set oXMLNodeList = oXMLDoc.selectNodes("/root/file[Filename='file2.txt']")
    13.  
    14. If oXMLNodeList.length <> 1 Then
    15. exit sub 'there is 0 or more files with same filename
    16. else
    17. 'now I want to read all atributes to string
    18.  
    19. For Each oXMLNode In oXMLNodeList(0).selectNodes("//Atribute")
    20. Atributes = Atributes & oXMLNode.Text  & ", "
    21. Next
    22.  
    23. end if

    It retunrs "ReadOnly, Hidden, Hidden, " but not only result for that file ("Hidden")

    I do know want to use loops, becourse it must be as fast as can be.

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: xml and xpath

    In short, what "//Atribute" does (more specifically, the "//"), is return all Atribute nodes, wherever they may exist within the XML tree.

    You should modify your code to:
    VB Code:
    1. If oXMLNodeList.length <> 1 Then
    2.     exit sub 'there is 0 or more files with same filename
    3. else
    4.     'now I want to read all atributes to string
    5.     For Each oXMLNode In oXMLNodeList(0).selectNodes("[B]Atribute[/B]")
    6.         Atributes = Atributes & oXMLNode.Text  & ", "
    7.     Next
    8. end if

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