Solved

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

Posted on 2013-12-27
10
1,104 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

734 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