tags:

views:

63

answers:

1

I am trying to write a program in java where in i can find all the xpath for the given xml.I found out the link on the internet xpath generator but it does not work when one element can repeat multipletimes for example if we have xml like the following :-

<?xml version="1.0" encoding="UTF-8"?>
<Report>
    <Name>
     <FirstName>A</FirstName>
     <LastName>B</LastName>
     <MiddleName>C</MiddleName>

    </Name>
    <Name>
     <FirstName>D</FirstName>
     <LastName>E</LastName>
     <MiddleName>S</MiddleName>
    </Name>
</Report>

It will produce xpaths :- /Report/Name/firstname for both firstname nodes. but the expected should be /Report/Name[1]/firstname and /Report/Name[2]/firstname

Any ideas?

+1  A: 

I think you may have to do this yourself.

Using a SAX parser will make it straightforward. Just maintain a stack of the elements you encounter and a count so you can increment the indexes (/Report/Name[1], /Report/Name[2]) easily.

Brian Agnew