XML 解析适用于 Android 2.2、2.3 但不适用于 ICS [英] XML parsing working fine with Android 2.2, 2.3 but not with ICS

查看:20
本文介绍了XML 解析适用于 Android 2.2、2.3 但不适用于 ICS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的 XML 代码是这样的.

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>

推荐答案

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

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

        }

这篇关于XML 解析适用于 Android 2.2、2.3 但不适用于 ICS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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