Solved

xml and vb6: Search for an attribute value and get its children

Posted on 2013-12-27
10
1,022 Views
Last Modified: 2013-12-27
So here is my xml and I want to search for a text "local:/1/xsl". If it matches, it should get me the file names only, if any.

xml
-----

<root>
	<directory name="local:/1/xsl" >
		<file name="1.xsl">  </file>
		<file name="2.xsl">  </file>
		<directory name="local:/2/xsl" >
			<file name="3">  </file>
			<file name="4">  </file>
		</directory>
	</directory>
</root>

Open in new window

0
Comment
Question by:Jammerules
  • 4
  • 3
  • 2
10 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 39741793
@Jammerules

Have you used the XML DOM object before in VB6?
0
 
LVL 18

Accepted Solution

by:
zc2 earned 500 total points
ID: 39741956
I don't have VB6, but I could give you an example in VBScript. I hope it will be easy to make it work in VB6 as well.
set xdDoc = CreateObject("MSXML2.DOMDocument")
xdDoc.load("the_xml_file.xml")
If xdDoc.parseError.errorCode <> 0 Then
     MsgBox "Error: " & xdDoc.parseError.reason & " in " & xdDoc.parseError.line & ":" & xdDoc.parseError.linepos
else
    dim s
    s = ""
    dim found ' a collection of all nodes having an attribute with the given value
    set found = xdDoc.selectnodes("//*[@*='local:/1/xsl']")
    for each node in found
        dim file_nodes ' a collection of children elements which have the tag "file"
        set file_nodes = node.selectNodes("file")
        for each file in file_nodes
            s = s & file.getAttribute("name") & vbCrLf
        next
    next
    MsgBox s
end if

Open in new window

0
 

Author Comment

by:Jammerules
ID: 39742212
aikimark - I am currently using XMLDOM and creating an object using
 Set doc = CreateObject("Msxml2.DOMDocument.3.0")

Open in new window

, but for some reason I am getting at what I want. Let my try zc2's code. Thanks!
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

Author Comment

by:Jammerules
ID: 39742250
ZC2,

I am not getting the exact results I am expecting. Here is the code I have with the same xml file as provided before.

Private Sub Command4_Click()
    Dim doc
    Dim file_nodes
    Set doc = CreateObject("MSXML2.DOMDocument")  'I have Microsoft XML, v6.0 dll referenced in the project
    doc.Load ("c:/Test.xml")
        
    Set found = doc.selectNodes("//*[@*='local:/2/xsl']")
    For Each Node In found
        
        Set file_nodes = Node.selectNodes("file")
        For Each file In file_nodes
            s = s & file.getAttribute("name") & vbCrLf
        Next
        MsgBox s
    Next
     
End Sub

Open in new window


What is happening is, when I give @*='local:/1/xsl' , then I am getting the file names. But when I give 'local:/2/xsl' then I am not getting its children's names. Also, watching "Found" does not show the directory "local:/2/xsl" as its child. What gives?
0
 
LVL 45

Expert Comment

by:aikimark
ID: 39742292
The reason the code isn't showing you the sub directory is because it is only looking for file nodes.
set file_nodes = node.selectNodes("file")

Open in new window


If you want both contained files and sub directories, you will need to add code to process the directory nodes.
        set dir_nodes = node.selectNodes("directory")
        For Each dir In dir_nodes
            s = s & "Directory: " & dir.getAttribute("name") & vbCrLf
        Next

Open in new window

0
 

Author Comment

by:Jammerules
ID: 39742301
My bad. I actually do not need the directory information at all. All I need is to check and see if there are any child files. But like I mentioned earlier,  that code is not showing the child node of "local:/2/xsl". Is there a  bug in the code?
0
 
LVL 18

Expert Comment

by:zc2
ID: 39742417
I've double checked, doc.selectNodes("//*[@*='local:/2/xsl']") returns the inner directory node.

If you want a flat list of files on any level below, replace
Set file_nodes = Node.selectNodes("file")
with
Set file_nodes = Node.selectNodes(".//file")
0
 

Author Comment

by:Jammerules
ID: 39742480
Works great. My sincere apologies for my input file did not have the appropriate node. I have corrected the file and it now shows what I want to see :) Thanks!
0
 
LVL 18

Expert Comment

by:zc2
ID: 39742501
You're welcome.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question