| Author |
How to get XPATH of a selected node in Java
|
Anubhav Anand
Ranch Hand
Joined: May 18, 2007
Posts: 341
|
|
Hi, I have an application in which on press of a button an XML is shown on the browser. Now when a user selects a node then i need to get the XPath of the selected node. How to do that? Can anybody help.... Thanks
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
|
Not so easy, since the XPath for a particular node can be a variety of different things. A node doesn't necessarily have an absolute XPath. Can't you just give your node unique id attributes?
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Anubhav Anand
Ranch Hand
Joined: May 18, 2007
Posts: 341
|
|
Yes I know that. I have a fixed xml file which will always be shown. And moreover it has repeated elements and is not having an ID. I got a handy javascript utility to do the same but it doesn't work well for large files so i had to leave the thought. But my application constarint is such that when a user selects a node then the corresponding XPath is to be stored for some manipulations. Is there any way out..... Thanks..
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
I got a handy javascript utility to do the same but it doesn't work well for large files so i had to leave the thought.
Do you want to do this in Java or JavaScript?
And moreover it has repeated elements...when a user selects a node then the corresponding XPath is to be stored for some manipulations
If you have repeated elements and you are using XPath to identify a particular element then if the user select one of the repreted element, they are implicitly selecting them all. Is this how you want it to work? [ May 18, 2007: Message edited by: Paul Sturrock ]
|
 |
Anubhav Anand
Ranch Hand
Joined: May 18, 2007
Posts: 341
|
|
I am sorry to have quoted about Javascript as i have already discarded it. I apologise for the same. Yes the xml has repeated nodes but the value at each node is unique. I have to get the XPath of the node that is selcted by the user. As i never know what value will be selected by the user i can't use it to get Xpath. I mean that both the XPath and the value are known at runtime and i have to get hold of both of them. Hope i made myself clear. Thanks..
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16482
|
|
Originally posted by Anubhav Anand: I am sorry to have quoted about Javascript as i have already discarded it.
But you said this XML is "shown in the browser". I assume this means you embed it in HTML in some way? (If not, what does it mean?) If so, then I don't see how you can use Java. That pretty much leaves Javascript as the only practical way of working with whatever you sent to the browser.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16482
|
|
Originally posted by Paul Sturrock: Not so easy, since the XPath for a particular node can be a variety of different things.
Well, that's true, there can be different XPath expressions that refer to the same node.
A node doesn't necessarily have an absolute XPath.
Sure it does. If nothing else you can use something like this:
/root/abc[3]/data[6]
[ May 18, 2007: Message edited by: Paul Clapham ]
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
|
D'oh! Of course. But doesn't that assume the ordering of the XML is preserved, and isn't ordering implementation-dependent?
|
 |
Anubhav Anand
Ranch Hand
Joined: May 18, 2007
Posts: 341
|
|
Yes, it is true that I am displaying the XML in browser but I am using XML parsers of jdom. Now, the application flow is like --->> The user now selects a node in the displayed XML and then I have to get the value at the current node and the XPath of the node to be stored in the DB. Is there any way out..?? Thanks in advance.
|
 |
 |
|
|
subject: How to get XPATH of a selected node in Java
|
|
|