Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 620
  • Last Modified:

XPath and Visual Basic 6

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
Jammerules
Asked:
Jammerules
  • 5
  • 2
1 Solution
 
Martin LissRetired ProgrammerCommented:
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
 
OnegaZhangCommented:
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
 
JammerulesAuthor Commented:
Thank you OnegaZhang, I will work on this code tomorrow and report with results.
0
Technology Partners: 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!

 
JammerulesAuthor Commented:
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
 
OnegaZhangCommented:
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
 
JammerulesAuthor Commented:
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
 
JammerulesAuthor Commented:
Awesome piece of code. Simple and straight to the point!
0
 
JammerulesAuthor Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now