I have a java program which will iterate all nodes from xml document.But i want to return/display each node's XPath with out using any DTD and XSD file standard.
Ok.I am following your idea now.But i have a duplicate nodes for particular node in the xml document.How can i get the exact path of the XPath of the node.
That's actually a fun little problem. I took a first stab at it here. As noted on that page, it's not perfect, but adequate for certain kinds of XML documents.
hi, i was able to get the xpath of the nodes.But it displays the value as zero for all the nodes.
This is the result i am getting.
path /C:/Documents/ Tag Name catalog Tag Name journal Tag Name article Tag Name title XPath //catalog[0]/journal[0]/article[0]/title[0] Tag Name author XPath //catalog[0]/journal[0]/article[0]/author[0] XPath //catalog[0]/journal[0]/article[0] XPath //catalog[0]/journal[0] Tag Name books Tag Name article Tag Name title XPath //catalog[0]/books[0]/article[0]/title[0] Tag Name author XPath //catalog[0]/books[0]/article[0]/author[0] XPath //catalog[0]/books[0]/article[0] XPath //catalog[0]/books[0] Tag Name journal Tag Name article Tag Name title XPath //catalog[0]/journal[0]/article[0]/title[0] Tag Name author XPath //catalog[0]/journal[0]/article[0]/author[0] XPath //catalog[0]/journal[0]/article[0] XPath //catalog[0]/journal[0] Tag Name book Tag Name article Tag Name title XPath //catalog[0]/book[0]/article[0]/title[0] Tag Name author XPath //catalog[0]/book[0]/article[0]/author[0] XPath //catalog[0]/book[0]/article[0] XPath //catalog[0]/book[0] Tag Name journal Tag Name article Tag Name title XPath //catalog[0]/journal[0]/article[0]/title[0] Tag Name author XPath //catalog[0]/journal[0]/article[0]/author[0] XPath //catalog[0]/journal[0]/article[0] XPath //catalog[0]/journal[0] XPath //catalog[0]
That's what the comment at the beginning of the code means - it will not work if the tags are not consecutive. Since between the first and second "journal" there's a "book" element, the counter will be reset. Fixing that is left as an exercise to the reader :-)
I have updated the sample code to deal with part of the problem. It now works with the example you posted. But it's still not perfect; see the code for an example where it fails. [ July 28, 2007: Message edited by: Ulf Dittmer ]
I am also getting same error. I have copied the sample code. Every thing is working fine except the error mentioned in at the beggining of the program....