?
Solved

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

Posted on 2013-12-27
10
Medium Priority
?
1,274 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 46

Expert Comment

by:aikimark
ID: 39741793
@Jammerules

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

Accepted Solution

by:
zc2 earned 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 46

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 19

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 19

Expert Comment

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
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…
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 …
Suggested Courses

719 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