wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes XPath Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XPath Question" Watch "XPath Question" New topic
Author

XPath Question

Sandeep Awasthi
Ranch Hand

Joined: Oct 23, 2003
Posts: 597
Hi All,

I want to find out attribute values of specific nodes in xml files using xpath.

Suppose I have following xml file and I want to find out year values for all book nodes using xpath. I was trying to do this in following way.
DocumentBuilderFactory domFactory = DocumentBuilderFactory
.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder build = domFactory.newDocumentBuilder();
Document d = build.parse("test.xml");
XPath xpath = XPathFactory.newInstance().newXPath();

XPathExpression ex = xpath.compile("//book[@year]");


NodeList result = (NodeList) ex.evaluate(d, XPathConstants.NodeSet);

for (int i = 0 ; i < result.getLength();++i) {
System.out.println("Year "i+" "+result.item(i).getNodeValue());
}
}


It gives me class cast exception.


My second question is

what is XPathConstants.DOM_OBJECT_MODEL and how to use it.

I want to do this using Xpath.


Please help me


Thanks and Regards


Rajesh
[ January 19, 2007: Message edited by: Rajesh Thakare ]

Sandeep
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XPath Question
 
Similar Threads
how to ignore whitespace in xml
Parsing XML file using Xpath in jdk1.4
Java: removing all of the attributes from XML Document
Save XML nodes in a String array for ChooseNodes
need help with practise questions.