在android中解析CDATA [英] Parsing CDATA in android

查看:18
本文介绍了在android中解析CDATA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析服务器上的 XML 我读取并解析它没有任何错误但我无法看到数据.

I'm parsing XML which is on the server I read it and parse it There is no any error But I'm unable to see the data.

这是我的 XML:

<BookData><book><Title><![CDATA[ABC]]></Title><AuthorFName1><![CDATA[A]]></AuthorFName1><AuthorLName1><![CDATA[B]]></AuthorLName1></book><book><Title><![CDATA[XYZ]]></Title><AuthorFName1><![CDATA[A]]></AuthorFName1><AuthorLName1><![CDATA[B]]></AuthorLName1></book>

我正在使用 DocumentBuilderFactory 看看我设置的代码

I'm using DocumentBuilderFactory see the code even I set

dbf.setCoalescing(true);

但仍然无法正常工作,请查看 DocumentBuilderFactory 的代码

But still not working please see the code for DocumentBuilderFactory

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) {
    Log.d("XML parse Error:", e.getMessage());
    return null;
} catch (SAXException e) {
    Log.d("Wrong XML File Structure", e.getMessage());
    return null;
} catch (IOException e) {
    Log.d("IOException", e.getMessage());
    return null;
}

推荐答案

试试这个,你只要把 InputSource 实例传给这个方法就可以了.

Try this, you just have to pass InputSource instance to this method and it works.

private void DOMParser(InputSource inputSource) {

        DocumentBuilderFactory dBuilderFactory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder documentBuilder = dBuilderFactory.newDocumentBuilder();
            Document dom = documentBuilder.parse(inputSource);

            // get the root element.....
            Element docElement = dom.getDocumentElement();
            Log.i("Root Element", docElement.getTagName());

            // now get the NodeList of root elements
            NodeList nodeList = docElement.getElementsByTagName("book");
            Log.i("NodeList Length", nodeList.getLength()+"");
            for (int i = 0; i < nodeList.getLength(); i++) {

                Element eleBook = (Element) nodeList.item(i);
                Log.i("Book Node", eleBook.getTagName());

                NodeList titleNode = eleBook.getElementsByTagName("Title");
                Element TitleEle = (Element) titleNode.item(0);
                Log.i("Title", "Title - "+TitleEle.getFirstChild().getNodeValue());

                NodeList AuthorFName1Node = eleBook.getElementsByTagName("AuthorFName1");
                Element AuthorFName1Ele = (Element) AuthorFName1Node.item(0);
                Log.i("AuthorFName1","AuthorFName1 - "+AuthorFName1Ele.getFirstChild().getNodeValue());

                NodeList AuthorFName11Node = eleBook.getElementsByTagName("AuthorLName1");
                Element AuthorFName11Ele = (Element) AuthorFName11Node.item(0);
                Log.i("AuthorLName1","AuthorLName1 - "+AuthorFName11Ele.getFirstChild().getNodeValue());
            }
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
    }

这篇关于在android中解析CDATA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆