473,898 Members | 3,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

All available xpath in XML document

9 New Member
I am using vb.net 2010 to get data from XML file, I was wondering if there is any way to find and print all available xpath of all nodes in the document. For example:
XML file is like :
Expand|Select|Wrap|Line Numbers
  1. <CATALOG>
  2.   <CD>
  3.     <TITLE>Empire Burlesque</TITLE>
  4.     <ARTIST>Bob Dylan</ARTIST>
  5.   </CD>
  6.   <CD>
  7.     <TITLE>Hide your heart</TITLE>
  8.     <ARTIST>Bonnie Tyler</ARTIST>
  9.   </CD>
The result should be something like :

/CATALOG
/CATALOG/CD
/CATALOG/CD/TITLE
/CATALOG/CD/ARTIST

I hope I'm clear enough..
May 26 '10 #1
9 3222
Dormilich
8,658 Recognized Expert Moderator Expert
I was wondering if there is any way to find and print all available xpath of all nodes in the document.
there’s more than just those 4 XPaths. you can have relative XPaths, you can have XPaths with conditions, with selectors, etc. etc.

what you possibly mean is a document tree representation.
May 26 '10 #2
oscar2
9 New Member
In fact I need those 4 paths and of course if there is another node inside say TITLE I need a path to that node as well. All paths should start from the Root node CATALOG in this case. It might be worth mentioning that I will then feed those paths to a list to give the user an easy way to select the node or node-set to be modified.
Thanks :)
May 26 '10 #3
jkmyoung
2,057 Recognized Expert Top Contributor
I've done this with xslt before; it's possible to do it with other languages as well. It's recursion; Pseudocode:
Expand|Select|Wrap|Line Numbers
  1. Start at document level. 
  2. call f(rootnode, "")
  3.  
  4.  
  5. f(Node n, String s){
  6.   For each child node of n {
  7.     path = s + \ + node name
  8.     print path
  9.     child.f(n, path)
  10.   }
  11. }
May 27 '10 #4
oscar2
9 New Member
Thanks I am working on something similar.. will post here if I get the same result with VB.net
Thanks again
May 27 '10 #5
oscar2
9 New Member
I am working with this right now, This recursive sub can easily find path of all children but it will miss any sibling!

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Public path As String
  3.  
  4.     Private Sub cmdRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRun.Click
  5.         Dim xDoc As XmlDocument = New XmlDocument
  6.         xDoc.Load("C:\Temp\file.xml")
  7.         Dim node As XmlNode = xDoc.SelectSingleNode("/catalog")
  8.         path = node.Name
  9.         GetPath(node)
  10.     End Sub
  11.  
  12.     Private Sub GetPath(ByVal node As XmlNode)
  13.         lst.Items.Add(path)
  14.  
  15.         If node.HasChildNodes = True Then  ' <==> If Not node.NodeType = XmlNodeType.Text Then 
  16.             path = path & "/" & node.FirstChild.Name
  17.             node = node.FirstChild
  18.             GetPath(node)
  19.         End If
  20.     End Sub
  21.  
  22. End Class
  23.  
Hope someone can build on this and give some insights.
May 27 '10 #6
jkmyoung
2,057 Recognized Expert Top Contributor
Lines 15 - 19.
You're only attacking the first child. You should be able to put just
if node.HasChildNo des
(remove the = true)

I'm not sure of the exact syntax in vb, but it goes something like:
Expand|Select|Wrap|Line Numbers
  1. dim children = node.ChildNodes
  2. For i = 0 To children.length
  3.   dim child = children[i]
  4.   path = path & "/" & child.Name 
  5.   GetPath(child) 
  6. Next i
  7.  
May 28 '10 #7
oscar2
9 New Member
Thanks.. i working on it :)
May 28 '10 #8
jkmyoung
2,057 Recognized Expert Top Contributor
Note, edited the above code, accidentally put GetPath(node) when I meant
GetPath(child)
May 28 '10 #9
oscar2
9 New Member
It is rather confusing right :)
Now at last I got it to work. Check this out:

Expand|Select|Wrap|Line Numbers
  1. Sub GetPaths(ByVal node As Xml.XmlNode, ByVal paths As List(Of String), Optional ByVal path As String = "")
  2.         path &= "/" & node.Name
  3.         If Not paths.Contains(path) Then
  4.             paths.Add(path)
  5.             lstPaths.Items.Add(path)
  6.         End If
  7.         For Each child As Xml.XmlNode In node.ChildNodes
  8.             If child.NodeType = Xml.XmlNodeType.Element Then
  9.                 GetPaths(child, paths, path)
  10.             End If
  11.         Next
  12.     End Sub
and...

Expand|Select|Wrap|Line Numbers
  1. Dim xDoc As New XmlDocument
  2. Dim paths As New List(Of String)
  3. GetPaths(xDoc.DocumentElement, paths)
  4. lstPaths.SelectedIndex = 0

Problem solved ;)
May 28 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
6833
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must be a relative path. 2. Poor performance
4
3306
by: Neil Cherry | last post by:
I've seen references to using <applets ... > and document.applets to access public Java functions from Javascript. Do I still use document.applets with <object ...>? BTW, the FAQS page seems to be broken. -- Linux Home Automation Neil Cherry ncherry@comcast.net http://home.comcast.net/~ncherry/ (Text only) http://hcs.sourceforge.net/ (HCS II)
4
6102
by: Vitali Gontsharuk | last post by:
Hallo! When using the XPATH document() function to load a new XML document, we are coming across problems, because XALAN seems to have problems with absolute paths. XALAN always assumes that the path is relative to the current directory. So if we e.g. are in "c:\xslt_scripts" and are trying to load an XML file from "c:\xml_files\test.xml" it ist trying to open a file with the following absolute path:...
3
5404
by: Richard Fritzler | last post by:
I was given the task of designing a complete web based document prep system. In simplest terms (using a msword explanation) create a database of merge fields, and a library of templates. Allow the webuser to select the template, merge his DB record, and produce a formatted document that can be printed or downloaded. We need to do this without specialized software on the client, since it will be universally available to webusers. We...
0
1430
by: Ramesh | last post by:
Hi, I have an xml file is as follows <?xml version="1.0" encoding="UTF-8"?> <Package Id="Test" xmlns="http://www.wfmc.org/2002/XPDL1.0" xmlns:xpdl="http://www.wfmc.org/2002/XPDL1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.wfmc.org/2002/XPDL1.0
2
2647
by: Praveen | last post by:
I have an xml with attribute value contains '\'. <Root> <I Attr= "A.B\TEST1INDX\C.L\TEST2INDX\M.N."/> how to do xpath for this attribute A="A.B\TEST1INDX\C.L\TEST2INDX\M.N." selectNodes("I") but this fails... how to escape this
2
7864
by: Andy | last post by:
Hi, I have an XML document that uses namespaces (it is from a Word 2007 file). I want to retrieve all the "t" elements that belong to the "w" namespace (<w:t>) using XPath from VB.NET 2003 (.NET framework 1.1). I've successfully loaded the document into a XmlDocument DOM parser (I can dump the contents using OuterXML). And, I've created a XmlNamespaceManager and assigned it the "w" namespace.
6
2020
by: Derek Hart | last post by:
I bring in an xml file into vb.net by using xmlDoc.LoadXml(XMLString) - I run xpath statements against the xml file to grab data from it, so I use, as an example, //Vehicles/Vehicles/@make to get the make of the car. But then I pass a specific node from xmlDoc into another function, not the whole xmlDoc, just a node from it. And if I run an xpath against it, I have to use .// (has a period at the beginning) so it does not grab info from...
1
2884
by: jesper_lofgren | last post by:
Hello, i need to help here. I have a xmlnode as you can se below (full xml is in the bottom). I wonder how i can get the values inside the CSammanstallningLista . <CSammanstallning Id="Omrade" DefaultInclude="0"> <Rubrik>Område</Rubrik> <Text>Kallhäll. Fabriksvägen, Hus S, JÄRFÄLLA</Text> </CSammanstallning>
0
9841
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11265
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10858
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10949
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8036
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7187
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5882
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6077
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4706
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.