Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to get xpath of each node?

843834Feb 15 2009 — edited Mar 25 2009
I need to read xml data and put it on the browser. I thought frist i will get xpath of all the elements and get its value calling xpath from my another java program.

Is there any better way to do it ? please suggest me?! In the following code the last element's number is
getting incremented where i want that to start from [0].

Can anyone please help me?

My code as follows:
book.xml
<?xml version = "1.0" encoding= "ISO-8859-1" standalone ="no"?>   
<book>   
    <person>   
          <first>Kiran</first>   
          <last>Pai</last>   
          <age>22</age>   
    </person>   
    <person>   
          <first>Bill</first>   
          <last>Gates</last>   
          <age>46</age>   
    </person>   
    <person>   
          <first>Steve</first>   
          <last>Jobs</last>   
          <age>40</age>   
    </person>   
</book>  
[/cdoe]

My java code as follows:
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

/**
* SAX handler that creates and prints XPath expressions for each element encountered.
*
* for above book.xml, my program will report
*
* //book[0]
//book[0]/person[0]
//book[0]/person[0]/first[0]---> This segment is working fine.
//book[0]/person[0]/last[0]
//book[0]/person[0]/last[0]/name[0]
//book[0]/person[0]/age[0]
//book[0]/person[1]
//book[0]/person[1]/first[1]->(this is wrong: should be //book[0]/person[1]/first[0])
//book[0]/person[1]/last[0]
//book[0]/person[1]/age[0]
//book[0]/person[2]
//book[0]/person[2]/first[2]-> (this is wrong: should be //book[0]/person[2]/first[0])
//book[0]/person[2]/last[0]
//book[0]/person[2]/age[0]
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 22 2009
Added on Feb 15 2009
4 comments
257 views