?
Solved

XPath and Visual Basic 6

Posted on 2013-12-14
8
Medium Priority
?
593 Views
Last Modified: 2013-12-19
So, how does one go about xpath'ing in vb6? Here is the tree structure I have and I want to get the File name and Size. I have the name that is coming out of a user form (from a text box) and based on that user input, I need to look for the values. Here is the tree struct:

<directory name="local:/ConditionalCallProcessing/xsl" read="true" write="true" audit="true" log="true" show="true" exec="true" del="true" dir="true" list="true">
<Access xmlns:func="http://exslt.org/functions">
<r>on</r>
<w>on</w>
<a>on</a>
<d>on</d>
<x>on</x></Access>
<file name="CustomExceptionMessage1.xsl">
<size>427</size>
<modified>2013-09-27 20:35:01</modified>

Open in new window


So, in the above example, I would like to search for the file CustomExceptionMessage1 and get its details. I also would like to print the path of the folder that contains it. How can I do this in vb6? Can the same code be applied to vb 2008?

TIA
Jay
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
  • 5
  • 2
8 Comments
 
LVL 49

Expert Comment

by:Martin Liss
ID: 39720303
Here's a site that might help. As far as writing code for VB6 and vb 2008 I doubt that it would be the same.
0
 
LVL 12

Expert Comment

by:OnegaZhang
ID: 39720658
VB6 is similar to VBScript. The following code snippet can query file name:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<directory name="local:/ConditionalCallProcessing/xsl" read="true" write="true" audit="true" log="true" show="true" exec="true" del="true" dir="true" list="true">
<Access xmlns:func="http://exslt.org/functions">
<r>on</r>
<w>on</w>
<a>on</a>
<d>on</d>
<x>on</x></Access>
<file name="CustomExceptionMessage2.xsl">
<size>427</size>
<modified>2013-02-22 20:35:01</modified>
</file>
<file name="CustomExceptionMessage1.xsl">
<size>427</size>
<modified>2013-09-27 20:35:01</modified>
</file>
</directory>
</root>

Dim doc
Set doc = CreateObject("Msxml2.DOMDocument.3.0")
doc.validateOnParse = False
doc.resolveExternals = False
doc.load("NewFile.xml")
strXPath = "/root/directory/file"  
Set file = doc.documentElement.selectSingleNode(strXPath)  
WScript.Echo(file.getAttribute("name") )
strXPath = "/root/directory/file[@name='CustomExceptionMessage1.xsl']"  
Set file = doc.documentElement.selectSingleNode(strXPath)  
WScript.Echo(file.getAttribute("name") )

Open in new window


VB2008 syntax is different from VC6. You may find VB.NET examples to use xpath.
0
 

Author Comment

by:Jammerules
ID: 39725819
Thank you OnegaZhang, I will work on this code tomorrow and report with results.
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: 39727415
Hello OnegaZhang,

Your solution works, but I am sorry I was not complety clear in my question. I noticed what I want is different after executing your code ... here is what I want,

1.  I want the file name e.g., CustomExceptionMessage2.xsl to be appended to the directory (its immediate parent). So the final output should look like this:
 
local://conditionalcallprocessing/xsl/CustomExceptionMessage2.xsl

Now, in the xml file there could be multiple directories within a directory. I am basically trying to show the xml tree in a listbox with path information. See attached xml file below. In that example xml,

1. there are 5 directories with the node name "directory".
2. If you look at the first directory called "local:/ConditionalCallProcessing", it has 2 sub directories and a file called "getAccount.html"
3. Inside the sub directories, there are again some files e.g. CustomExceptionMessage1.xsl under the first sub directory local:/ConditionalCallProcessing/xsl


So, what I want is, I want the path constructed to the files;
for CustomExceptionMessage1.xsl path will be local:/ConditionalCallProcessing/xsl/CustomExceptionMessage1.xsl
for getAccount.html path will be
local:/ConditionalCallProcessing/getAccount.html

I want to do this for all the consecutive directories and their files. And finally, if you see at the end, there are also some lingering files (AAAInfo.xml and getAccount.html) whose parent is not a "directory" but it is "location". So, I want any files that are under location also.

Thank you so much for any help offered.
FileDetails.xml
0
 
LVL 12

Accepted Solution

by:
OnegaZhang earned 2000 total points
ID: 39728517
Hope you can work out with the following code snippets.
Dim doc
Set doc = CreateObject("Msxml2.DOMDocument.6.0")
doc.validateOnParse = False
doc.resolveExternals = False
doc.async = False
doc.load("FileDetails.xml")
strXPath = "//*/file"  
Set allfiles = doc.documentElement.selectNodes(strXPath) 
For Each file In allfiles	
  if StrComp(file.parentNode.nodeName, "directory")=0 Then
  	WScript.Echo(file.parentNode.getAttribute("name") & "/" & file.getAttribute("name") )
  End If  
Next

Open in new window

0
 

Author Comment

by:Jammerules
ID: 39728600
Thanks!~

Works like a champ. Although, I added a small piece of code right after the For loop to capture the file elements that belong to "Location" node also.

For Each file In allfiles
  If StrComp(file.parentNode.nodeName, "location") = 0 Then
   MsgBox (file.parentNode.getAttribute("name") & "/" & file.getAttribute("name"))
  End If
Next

Open in new window


Thank you so much again!
0
 

Author Closing Comment

by:Jammerules
ID: 39728602
Awesome piece of code. Simple and straight to the point!
0
 

Author Comment

by:Jammerules
ID: 39731155
OnegaZhang - If you are interested, I have one more question :)

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_28322848.html
0

Featured Post

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!

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

762 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