Solved

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

Posted on 2013-12-27
10
990 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
 

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Word files to Wordpress Posts 2 43
Modify a small python script 19 96
Adoquery sql  left join does not work 25 80
Advice in Xamarin 21 52
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now