3

how to parser the following XML using DOM PARSER

<Result>
<Status>OK</Status>
<All_BookDetails>
<BookAuthor>Mohammadi Reyshahri</BookAuthor> 
<BookRating>0</BookRating>
<BookDescription>Islamic belief and ideology</BookDescription>
<DatePublished>May  1 1992 12:00AM</DatePublished>
<BookTitle>Are You Free or Slave</BookTitle>
<BookID>171</BookID>
<BookCode>EN171</BookCode>
<BookImage>1.jpg</BookImage>
<TotalPages>164</TotalPages>
</All_BookDetails>
</Result>

i want to get the values of BookAuthor, BookRating, BookDescription,DatePublished, BookTitle, BookID, BookCode, BookImage TotalPages

how can i do this. I tried to parse the above XML selecting All_BookDetails as parent node but nodelist returning me the 0 in length

thanks

4
  • 2
    mkyong.com/java/how-to-read-xml-file-in-java-dom-parser. A place where you can start. Commented Feb 15, 2013 at 7:11
  • in which device and its API level? I smell it just same issue which I'm facing, look here Commented Feb 15, 2013 at 7:12
  • Please show some code you tried Commented Feb 15, 2013 at 7:13
  • I am so sorry guys i was commenting a mistake. i was not selecting the <All_BookDetails> as the parent. It was bug here in my code. Commented Feb 15, 2013 at 7:20

1 Answer 1

3

Getting XML DOM element

public Document getDomElement(String xml) {
    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setCoalescing(true);
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is);

    } catch (ParserConfigurationException e) {
        return null;
    } catch (SAXException e) {
        return null;
    } catch (IOException e) {
        return null;
    }

    return doc;

}

then I tried this and its worked

Document doc = parser.getDomElement(XMLString);
            NodeList nl = doc.getElementsByTagName("All_BookDetails");

            progressDialog.setCancelable(true);
            Element e = (Element) nl.item(0);
            BookRating = (Integer.valueOf(parser.getValue(e,
                        "BookAuthor")));

            BookTitle = parser.getValue(e, "BookTitle");
            BookAuthor = parser.getValue(e, "BookAuthor");
            BookPublishDate = parser.getValue(e, "DatePublished");
            BookDescription = parser.getValue(e, "BookDescription");
            bookID = parser.getValue(e, "BookID");
            bookCode = parser.getValue(e, "BookID");
            bookPageCount = parser.getValue(e, "TotalPages");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.