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
Solved

XPath and Visual Basic 6

Posted on 2013-12-14
8
547 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
  • 5
  • 2
8 Comments
 
LVL 46

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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
simplest php form 3 80
Is doing tutor.com teaching in my situation advisable? 2 108
Using Third Party DLL with Access VBA 14 38
learn programming 8 27
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

792 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