Solved

Programmatically obtaining the XPATH of an XML Node : Java 1.4

Posted on 2006-11-30
3
798 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
[X]
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
3 Comments
 
LVL 12

Accepted Solution

by:
enachemc earned 200 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

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…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

737 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