|
-
Mar 24th, 2005, 04:48 AM
#1
Thread Starter
Hyperactive Member
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:
Dim oXMLDoc As MSXML2.DOMDocument40
Dim oXMLNode As MSXML2.IXMLDOMNode
Dim oXMLNodeList As MSXML2.IXMLDOMNodeList
Set oXMLDoc = New MSXML2.DOMDocument40
oXMLDoc.async = False
oXMLDoc.resolveExternals = True
oXMLDoc.Load ("C:\files.xml")
oXMLDoc.setProperty "SelectionLanguage", "XPath"
Set oXMLNodeList = oXMLDoc.selectNodes("/root/file[Filename='file2.txt']")
If oXMLNodeList.length <> 1 Then
exit sub 'there is 0 or more files with same filename
else
'now I want to read all atributes to string
For Each oXMLNode In oXMLNodeList(0).selectNodes("//Atribute")
Atributes = Atributes & oXMLNode.Text & ", "
Next
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.
-
Mar 24th, 2005, 12:13 PM
#2
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:
If oXMLNodeList.length <> 1 Then
exit sub 'there is 0 or more files with same filename
else
'now I want to read all atributes to string
For Each oXMLNode In oXMLNodeList(0).selectNodes("[B]Atribute[/B]")
Atributes = Atributes & oXMLNode.Text & ", "
Next
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|