Places API缺少标记Android

时间:2013-01-25 20:57:12

标签: java android xml-parsing google-api google-places-api

我正在使用Google放置api,我正在尝试解析XML数据以获得包括评级在内的许多内容。唯一的问题是有时候没有评级标签(如下所示)。我该如何解决这个问题?

<name>Zari</name>
<type>restaurant</type>
<type>food</type>
<type>establishment</type>
<formatted_address>212-214 Ifield Drive,            Crawley, United Kingdom</formatted_address>
<geometry>
<location>
 <lat>51.1218980</lat>
   <lng>-0.2135630</lng>
 </location>
 </geometry>
<rating>3.3</rating>
 </result>

 <result>
<name>Happy Meeting</name>
<type>restaurant</type>
<type>food</type>
<type>establishment</type>
<formatted_address>
<geometry>
<location>
<lat>51.1161600</lat>
<lng>-0.1844890</lng>
</location>
</geometry>
</result>
<result>

1 个答案:

答案 0 :(得分:0)

只需检查您是否得到口粮。例如。如果您正在解析像这样的数据

    String temp=null;
    int rating;
        if (localName.equals("rating")){ //this code won't work if rating tag is
                                        // not present thus temp will be null

                    temp = attributes.getValue("rating");
                    rating = Integer.parseInt(temp);
    }

现在在代码中进一步检查

if(temp==null){
rating=0; //or any that you want
}