aspose file tools*
The moose likes XML and Related Technologies and the fly likes how to retrieve attribute value using xpath 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 to retrieve attribute value using xpath" Watch "how to retrieve attribute value using xpath" New topic
Author

how to retrieve attribute value using xpath

Sandeep Parmar
Greenhorn

Joined: Aug 06, 2008
Posts: 10
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

Joined: May 27, 2008
Posts: 225

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

Joined: Aug 06, 2008
Posts: 10
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
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to retrieve attribute value using xpath