-2

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.

dirkk
  • 6,160
  • 5
  • 33
  • 51
Muthu kader
  • 119
  • 1
  • 4
  • 13
  • Post the exception trace – Sumit Desai May 31 '13 at 13:04
  • 4
    i don't think the method `getElementsByTagName` accepts Xpath expressions. You could better do something like this `XPathExpression expr = xpath.compile("/bookstore/book/int [@name='price']/url"); NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);` . Take a look at this http://stackoverflow.com/questions/2811001/how-to-read-xml-using-xpath-in-java – fGo May 31 '13 at 13:07
  • [The Java XPath API](http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html) on developerWorks – McDowell May 31 '13 at 22:03
  • I need the clear code for my above problem.I need to read the value of author,price of each doc tag.Give me the sample code – Muthu kader Jun 01 '13 at 05:11

1 Answers1

0

public static ArrayList gettheXMLvalues(Document xmlDocument, String author) throws Exception {

    ArrayList<String> result = new ArrayList<String>();

    List<Element> elementList = xmlDocument.selectNodes("//str[@name='author']");

    if (elementList == null) {
        return result;
    }

    ArrayList<Element> listAuthor = new ArrayList<Element>();

    for (int i = 0; i < elementList.size(); i++) {
        Element el = elementList.get(i);
        if (el.getText().equalsIgnoreCase(author)) {
            listAuthor.add(el);
        }
    }

    if (listAuthor.size() == 0) {
        return result;
    }
    else {
        String authorLine = "";

        for (int i = 0; i < listAuthor.size(); i++) {

            Element element = listAuthor.get(i);

            Element price = (Element)element.getParent().selectSingleNode("./int[@name='price']");
            Element subauthor = (Element) element.getParent().selectSingleNode("./str[@name='subauthor']");

            authorLine = "author-" + author + ",price-" + price.getText() + ",subauthor-" + subauthor.getText();

            result.add(authorLine);
        }
    }

    return result;
}
sghaier ali
  • 433
  • 2
  • 15