Solved

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

Posted on 2013-12-27
10
957 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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. 
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

759 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