?
Solved

Programmatically obtaining the XPATH of an XML Node : Java 1.4

Posted on 2006-11-30
3
Medium Priority
?
804 Views
Last Modified: 2008-02-26
Hi All

Given an XML document and any given node inside the XML, how do I obtain the XPATH for that particular node from the 'root' node using Java code ?

For instance, if I have an XML like this

<root>
  <level1>
    <sublevel11> sss </sublevel11>
    <sublevel12>
         <sublevel12A> xxx </sublevel12A>
         <sublevel12B> xxx </sublevel12B>
    </sublevel12>
  </level1>
</root>

Now, when I am at <sublevel12A>, I need my output as "root/level1/sublevel12/sublevel12A".

How do I write Java code that can do this ?

Thanks in Advance !
0
Comment
Question by:netuser1976
1 Comment
 
LVL 12

Accepted Solution

by:
enachemc earned 800 total points
ID: 18044781
<root>
  <level1>
    <sublevel11> sss </sublevel11>
    <sublevel12>
         <sublevel12A> xxx </sublevel12A>
         <sublevel12B> xxx </sublevel12B>
    </sublevel12>
    <sublevel12>
         <sublevel12A> xxx </sublevel12A>
         <sublevel12B> xxx </sublevel12B>
    </sublevel12>
  </level1>
</root>

if the above is the XML in cause, then you XPath would fail ... as there are 2 XML elements that match the required name. You will have to do something like: /root/level1[position()=1]/sublevel12[position()=1]/sublevel12A[position()=1]

You will have to start from the node, write it's position in the parent, then continue with it's parent in a while loop (iterating through parents until the root is reached - it has no parent).
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

850 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