• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

How can i get XPath of the XML Node using Java API without DTD and XSD

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
saravanan periasamy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ulf Dittmer,

I will try that.

Regards,
Saravanan.P
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Ulf Dittmer,

Could you please help on the above issue.

Thanks in Advance,
Saravanan.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok let me try..thank you :-)
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic