How to parse XML with a DOM parser

how to parse 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 by selecting All_BookDetails

as parent node, but nodelist

giving me 0 in length

thank

+3


source to share


1 answer


Retrieving an 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 worked it

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");

      

+3


source







All Articles