1

我正在使用不适用于 ICS 版本的 Android 的 XML 解析从服务器获取一些数据。请告诉我我做了什么更正,以便我也应该在 ICS 上运行......(它在较低的情况下工作正常版本)。这是我的代码

try {
        URL url = new URL(
                "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(url.openStream()));
        doc.getDocumentElement().normalize();
        NodeList nodeList = doc.getElementsByTagName("file");

        namephoto = new String[nodeList.getLength()];
        for (int i = 0; i < nodeList.getLength(); i++) {

            Node node = nodeList.item(i);
            Element fstElmnt = (Element) node;
            NodeList nameList = fstElmnt.getElementsByTagName("file");
            Element nameElement = (Element) nameList.item(0);
            nameList = nameElement.getChildNodes();
            namephoto[i] = ((Node) nameList.item(0)).getNodeValue();

        }
    } catch (Exception e) {
        Log.e("name", "" + e);
    }
    photobitmap = new Bitmap[namephoto.length];

    setPhotoBackground(namephoto[index_photo]);

我的 XML 代码是这样的。

<?xml version="1.0"?>
-<root><file>1 a.JPG</file><file>2 b.JPG</file><file>3 c.JPG</file><file>4 d.JPG</file>  </root>
4

1 回答 1

2

我自己有解决方案。这是与 Android 4.0 以及其他 android 版本兼容的代码...只需像这样更改 for 循环。

for (int i = 0; i < nodeList.getLength(); i++) {
            Node name = nodeList.item(i);
            NodeList nodeEle = name.getChildNodes();
            namephoto[i] = ((Node) nodeEle.item(0)).getNodeValue();

        }
于 2012-08-03T14:49:20.813 回答