wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes How can i get XPath of the XML Node using Java API without DTD and XSD Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How can i get XPath of the XML Node using Java API without DTD and XSD" Watch "How can i get XPath of the XML Node using Java API without DTD and XSD" New topic
Author

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
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: 41601
    
  55
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:


Ping & DNS - my free Android networking tools app
saravanan periasamy
Greenhorn

Joined: Feb 06, 2005
Posts: 22
Dear Ulf Dittmer,

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: 41601
    
  55
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
Thanks Ulf Dittmer,

I will try that.

Regards,
Saravanan.P
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
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
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
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]

The xml file used is:

<?xml version="1.0" encoding="utf-8"?>
<catalog>
<journal title="Oracle Magazine" publisher="Oracle Publishing" edition="July-August 2005">
<article section="Technology">
<title>Tuning Undo Tablespace</title>
<author>Kimberly Floss</author>
</article></journal>
<books title="Oracle Magazine" publisher="Oracle Publishing" edition="July-August 2005">
<article section="Developer">
<title>Using PHP 5 with Oracle XML DB</title>
<author>Yuli Vasiliev</author>
</article></books>
<journal title="Oracle Magazine" publisher="Oracle Publishing" edition="July-August 2005">
<article section="Developer">
<title>Using PHP 5 with Oracle XML DB</title>
<author>Yuli Vasiliev</author>
</article></journal>
<book title="Oracle Magazine" publisher="Oracle Publishing" edition="July-August 2005">
<article section="Developer">
<title>Using PHP 5 with Oracle XML DB</title>
<author>Yuli Vasiliev</author></article>
</book><journal title="Oracle Magazine" publisher="Oracle Publishing" edition="July-August 2005">
<article section="Developer">
<title>Using PHP 5 with Oracle XML DB</title>
<author>Yuli Vasiliev</author>
</article></journal></catalog>


Could you please help me on this.
saravanan periasamy
Greenhorn

Joined: Feb 06, 2005
Posts: 22
hi Ulf Dittmer,

Could you please help on the above issue.

Thanks in Advance,
Saravanan.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
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
ok let me try..thank you :-)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
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
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
Planet Bob
Greenhorn

Joined: May 27, 2010
Posts: 2
To fix this issue, store the count for XPath itself rather than the tag.

ALSO NOTE that XPath has "1" as starting index, not the ZERO.

 
Consider Paul's rocket mass heater.
 
subject: How can i get XPath of the XML Node using Java API without DTD and XSD