XML解析工作正常了Android 2.2,2.3,但不与ICS [英] XML parsing working fine with Android 2.2, 2.3 but not with ICS

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

问题描述

我使用的XML解析不工作与ICS版本Android.here的获取来自服务器的一些数据,我请你告诉我我该做出什么调整,这样我也应该对ICS运行...(它的工作细跟低版本)。这是我的code

 尝试{
        网址URL =新的URL(
                xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx);
        DocumentBuilderFactory的DBF = DocumentBuilderFactory.newInstance();
        DocumentBuilder的DB = dbf.newDocumentBuilder();
        文档的文档= db.parse(新的InputSource(url.openStream()));
        doc.getDocumentElement()归()。
        节点列表节点列表= doc.getElementsByTagName(文件);

        namephoto =新的String [nodeList.getLength()];
        的for(int i = 0; I< nodeList.getLength();我++){

            节点node = nodeList.item(ⅰ);
            元素fstElmnt =(元)节点;
            节点列表名单= fstElmnt.getElementsByTagName(文件);
            元件nameElement =(元件)nameList.item(0);
            名单= nameElement.getChildNodes();
            namephoto [I] =((节点)nameList.item(0))getNodeValue()。

        }
    }赶上(例外五){
        Log.e(名,+ E);
    }
    photobitmap =新的位图[namephoto.length]

    setPhotoBackground(namephoto [index_photo]);
 

我的XML code这样的。

 < XML版本=1.0&GT?;
 - <根><文件> 1 A.JPG< /文件><文件> 2 B.JPG< /文件><文件> 3 C.JPG< /文件><文件> 4 D.JPG< /文件> < /根>
 

解决方案

我已经得到了解决自己..这里是code这是与Android 4.0和Android的版本休息兼容只是...改变for循环是这样的。

 的for(int i = 0; I< nodeList.getLength();我++){
            节点名= nodeList.item(我);
            节点列表nodeEle = name.getChildNodes();
            namephoto [I] =((节点)nodeEle.item(0))getNodeValue()。

        }
 

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>

解决方案

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天全站免登陆