1

我有这个xml文件。

测试.xml

<?xml version="1.0" encoding="UTF-8"?>
<playList baseUrl= "Webaddress">
<file name="xxxxxxx.xx1" showTime="YYYY-MM-DD HH:MM" />
<file name="xxxxxxx.xx2" showTime="YYYY-MM-DD HH:MM" />
</playList>

这个文件位于sdcard。现在我想解析这个 xml。

我实现了许多演示但获得了成功

我试图整合这个例子,但得到IO 异常。

请给我一些建议或代码片段。

4

2 回答 2

2

请参阅使用 XmlPullParser 从 sdcard 解析 xml,其中它们使用您正在寻找的相同。让我知道进一步的查询。

于 2013-09-06T09:45:31.123 回答
1

这样的事情有助于开始

    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try 
    {
        DocumentBuilder db = dbf.newDocumentBuilder();

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

    } catch (ParserConfigurationException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    } catch (SAXException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    } catch (IOException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    }
        // return DOM
    return doc;

之后你为此创建一个函数,然后你有你的文档

    Document doc = parser.getDomElement(xml); // getting DOM element

然后你可以得到这样的元素,例如:

NodeList n1_item = doc.getElementsByTagName(KEY_ITEM_DESC);
于 2013-09-06T09:36:44.377 回答