且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用DOM解析器解析XML

更新时间:2023-02-26 10:42:00

获取XML DOM元素

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