Android 4.0 冰淇淋三明治解析器错误

发布于 2024-12-14 19:37:58 字数 1853 浏览 1 评论 0原文

我制作了一个从 Api 读取信息的应用程序。

链接:http://api.amp.active.com/camping/campground/details?contractCode=CO&parkId=50032&api_key=2chxq68efd4azrpygt5hh2qu

以下是我的代码:

NodeList list = element.getElementsByTagName("detailDescription");

        Log.i("ZealDeveloper","I M In detail   "+list.getLength());             
        if(list != null && list.getLength() > 0){


            for(int i = 0; i < list.getLength(); i++){

                entry = (Element) list.item(i);

                description = entry.getAttribute("description");
                drivingDirection = entry.getAttribute("drivingDirection");
                latitude=entry.getAttribute("latitude");
                longitude=entry.getAttribute("longitude");

            }               
        }

        NodeList list1 = element.getElementsByTagName("amenity");

        Log.i("ZealDeveloper","I M In 2   "+list1.getLength());

        if(list1 != null && list1.getLength() > 0){

            for(int i = 0; i < list1.getLength(); i++){
            entry = (Element) list1.item(i);

            nameAmenity = entry.getAttribute("name");

            listAmenity.add(nameAmenity);

            }               

            arrAmenity = listAmenity.toArray(new String[listAmenity.size()]);

            StringBuilder builder = new StringBuilder();

            for ( int i = 0; i < arrAmenity.length; i++ ){

                 builder.append(arrAmenity[i]+"\n");

            }
            txtAmenity.setText(builder);
    }
#

我正在获取 list.getLength() as 0(在以前的 Android 版本中获取 1) ,所以解析器这个条件。对于便利设施标签,我得到了所需的列表大小。

I have made a application which reads information from an Api.

Link : http://api.amp.active.com/camping/campground/details?contractCode=CO&parkId=50032&api_key=2chxq68efd4azrpygt5hh2qu

Following is my code :

NodeList list = element.getElementsByTagName("detailDescription");

        Log.i("ZealDeveloper","I M In detail   "+list.getLength());             
        if(list != null && list.getLength() > 0){


            for(int i = 0; i < list.getLength(); i++){

                entry = (Element) list.item(i);

                description = entry.getAttribute("description");
                drivingDirection = entry.getAttribute("drivingDirection");
                latitude=entry.getAttribute("latitude");
                longitude=entry.getAttribute("longitude");

            }               
        }

        NodeList list1 = element.getElementsByTagName("amenity");

        Log.i("ZealDeveloper","I M In 2   "+list1.getLength());

        if(list1 != null && list1.getLength() > 0){

            for(int i = 0; i < list1.getLength(); i++){
            entry = (Element) list1.item(i);

            nameAmenity = entry.getAttribute("name");

            listAmenity.add(nameAmenity);

            }               

            arrAmenity = listAmenity.toArray(new String[listAmenity.size()]);

            StringBuilder builder = new StringBuilder();

            for ( int i = 0; i < arrAmenity.length; i++ ){

                 builder.append(arrAmenity[i]+"\n");

            }
            txtAmenity.setText(builder);
    }

#

I am Getting list.getLength() as 0(was getting 1 in pervious versions of android) , so the parser this condition. For amenity tag i m getting the desired list size.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

默嘫て 2024-12-21 19:37:58

我能想到的唯一原因是“detailDescription”已经是文档的根目录,因此可能 element 是您正在寻找的标签。它没有任何名为“detailDescription”的子项,因此 getElementsByTagName("detailDescription") 返回空列表。因此,将代码的前半部分更改如下:

    Log.i("ZealDeveloper","I M In detail   " + element.getTagName()); 
    if(element.getTagName().equalsIgnoreCase("detailDescription")) {

            description = element.getAttribute("description");
            drivingDirection = element.getAttribute("drivingDirection");
            latitude = element.getAttribute("latitude");
            longitude = element.getAttribute("longitude");
    }  

    /* rest of your code...*/

The only reason I can think of is that "detailDescription" is already root of the document so probably element is the tag you are seeking for. It doesn't have any children with the name "detailDescription" so getElementsByTagName("detailDescription") returns empty list. So change first half of your code as follows:

    Log.i("ZealDeveloper","I M In detail   " + element.getTagName()); 
    if(element.getTagName().equalsIgnoreCase("detailDescription")) {

            description = element.getAttribute("description");
            drivingDirection = element.getAttribute("drivingDirection");
            latitude = element.getAttribute("latitude");
            longitude = element.getAttribute("longitude");
    }  

    /* rest of your code...*/
笑着哭最痛 2024-12-21 19:37:58

我有同样的问题。我从 DOM 切换到 XPath。更多此处

I had the same issue. I switch from DOM to XPath. More here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文