XML parsing working fine with Android 2.2, 2.3 but not with ICS

冷暖自知 提交于 2019-12-02 07:18:45

问题


I am fetching the some data from the server using XML parsing that is not working with ICS version of Android.here is my please tell me what correction do I make so that I should also run on ICS...(It's working fine with lower versions). Here is my code

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

My XML code like this.

<?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>

回答1:


i have got the solution myself.. Here is the code which is compatible with Android 4.0 as well as rest of the android versions...Just change the for loop like this.

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

        }


来源:https://stackoverflow.com/questions/11691550/xml-parsing-working-fine-with-android-2-2-2-3-but-not-with-ics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!