• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XPath Question

 
Ranch Hand
Posts: 597
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic