?
Solved

Programmatically obtaining the XPATH of an XML Node : Java 1.4

Posted on 2006-11-30
3
Medium Priority
?
801 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 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

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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

765 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