I am stuck with this task for the past two days and I did not get any clear solution through different articles. So kindly give me a step by step code to retrieve the XPath values present in XML.
My XML is
<bookstore>
<book>
<int name="sno">1</int>
<str name="author">J K. Rowling</str>
<int name="price">29.99</int>
<str name="subauthor">J K</str>
</book>
<book>
<int name="sno">2</int>
<str name="author">J K. Rowling</str>
<int name="price">29.99</int>
<str name="subauthor">hamilton</str>
</book>
</bookstore>
In this XML I need each author, price and subauthor values. My expected result is:
(author-J K. Rowling,price-29.99,subauthor-j k)
And then how to get the last value of the subauthor from this XML.
My java code to get this values is not working. Its throws an exception only.
public gettheXMLvalues() {
try {
NodeList nodeLst1 = doc.getElementsByTagName("doc");
for (int i = 0; i < nodeLst1.getLength(); i++) {
Node node = nodeLst1.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
NodeList nodes = element.getElementsByTagName("//str[@name='author']").item(0).getChildNodes();
node = (Node) nodes.item(0);
System.out.println("ELEMETS " + element.getTextContent());
System.out.println("Author" + node.getNodeValue());
}
} catch(Exception e){
system.out.println("Exception "+e);
}
}
Kindly give me a solution to get the value of author, price and subauthor of each book.I tried lots of things for getting the result, but unfortunately I did not get the result. Kindly give me the clear solution.