• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to retrieve attribute value using xpath

 
Sandeep Parmar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello experts,
I have a following xml chunk

<image file="/articles/engineering/boxerengine/preview_282x160.jpg">
<tout name="copyright"></tout>
<tout name="license"></tout>
<tout name="photographer"></tout>
<tout name="description"></tout>
</image>

I want to retrieve value of attribute file from absolute node image.
I tried using xpath='/image/@file' It gives output as file="/articles/engineering/boxerengine/preview_282x160.jpg"

I just want value "/articles/engineering/boxerengine/preview_282x160.jpg"
I tried using xpath='/image/@file/text()' . It does not work

Please help.

Thanks,
Sandeep Parmar
 
Carey Evans
Ranch Hand
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should retrieve the attribute from XPath using the STRING type, not NODE. If this doesn't make sense, can you please post a sample of the Java code that's calling XPath?
 
Sandeep Parmar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Carey,
Thanks for your reply.
Following is the java code. I am using XOM parser.
java code :

Builder parser = new Builder();
Document doc = parser.build(xmlFile);
Nodes titles = doc.query(xPath);
//xpath = ''/image/@file/text()"
for (int i = 0; i < titles.size(); i++) {
strChunk = strChunk.append(titles.get(i).toXML());
}
return strChunk.toString();
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sandeep Parmar:
I am using XOM parser.
Aha. Well, don't use the toXML() method then. Look for some other method to get the value of an attribute in XOM.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!