How can i get XPath of the XML Node using Java API without DTD and XSD
saravanan periasamy
Greenhorn
Joined: Feb 06, 2005
Posts: 22
posted
0
Dear All,
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.
Please help me!.
Thanks in Advance, Saravanan.P
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 32765
posted
0
Usually you'd use XPath to get a node from an XML/DOM document, not the other way around.
If you're using SAX, you could create XPath expressions something like this:
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.
Thanks, Saravanan
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 32765
posted
0
In that case you have to count the nodes, and add an "[...]" part to the XPath to select the proper one (after the element name).
saravanan periasamy
Greenhorn
Joined: Feb 06, 2005
Posts: 22
posted
0
Thanks Ulf Dittmer,
I will try that.
Regards, Saravanan.P
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 32765
posted
0
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.
saravanan periasamy
Greenhorn
Joined: Feb 06, 2005
Posts: 22
posted
0
Ulf Dittmer,
Its looking good,I think it may fit for my requirement.Let me try that.
Thanks a lot, Saravanan.P
saravanan periasamy
Greenhorn
Joined: Feb 06, 2005
Posts: 22
posted
0
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 :-)
saravanan periasamy
Greenhorn
Joined: Feb 06, 2005
Posts: 22
posted
0
ok let me try..thank you :-)
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 32765
posted
0
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 ]
Aditi Gandy
Ranch Hand
Joined: Apr 23, 2008
Posts: 37
posted
0
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....
Can any one please help me?
Please
Thanks & Regards,
Author
subject: How can i get XPath of the XML Node using Java API without DTD and XSD