• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Find XPath

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I know a xml Element, how can I programaticly find xPath for this Element? Is there any free or not free software to do this? I am not looking for GUI tool. I mean PROGRAMATICALLY find xpath. Thanks
 
Ranch Hand
Posts: 1241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that a given node may have several different XPaths to get to it. For example, take the following XML:


The node containing "bob" could be reached by several different XPaths:


and so on.

There may be tools out there to provide XPaths for a particular node, but it may just be simpler to write your own method - that way you can get the exact XPath that you want. Besides, this seems like a bit of an odd way to go about things - normally XPaths are used to find a node, not the other way around!
[ March 31, 2005: Message edited by: Dave Lenton ]
 
chack boom
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean I know Element. I want to find xpath. For example, element.getXPath();
 
Dave Lenton
Ranch Hand
Posts: 1241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even so, you'll still have a problem because there are usually more than one XPath for any given node or element. One option would be to write your own method for getting an XPath - you could have a method that takes an node or element and uses methods such as getParentNode() to work out the path back to the root. This may need some way of working out which child of the parent the current node/element is (e.g. first, second etc). Throw in a bit of recursion, and hey presto, a simple XPath. Alternatively this same kind of thing could be achieved using XSLT.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic