[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Programmatically obtaining the XPATH of an XML Node : Java 1.4

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
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…
Suggested Courses

656 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