【问题标题】:XML attribute value not getting fetched未获取 XML 属性值
【发布时间】:2016-08-01 10:17:27
【问题描述】:

我正在尝试解析下面的 XML 文件:

<current>
<city id="1273840" name="Connaught Place">
<coord lon="77.22" lat="28.63"/>
<country>IN</country>
<sun rise="2016-04-11T00:29:24" set="2016-04-11T13:15:01"/>
</city>
<temperature value="308.15" min="308.15" max="308.15" unit="kelvin"/>
<humidity value="17" unit="%"/>
<pressure value="1010" unit="hPa"/>

我能够成功获取国家标签(“IN”)的值,但无法获取属性“值”的值或温度、压力或上述任何其他标签的任何属性值。我得到null 作为值。 以下是我写的代码:

protected String doInBackground(Void... params) {
        try {

            URL u = new URL(URL);
            HttpURLConnection con = (HttpURLConnection) u.openConnection();
            con.setReadTimeout(10000);
            con.setConnectTimeout(10000);
            con.setRequestMethod("GET");
            con.setDoInput(true);
            con.connect();
            InputStream i = con.getInputStream();
            xf = XmlPullParserFactory.newInstance();
            xp = xf.newPullParser();
            xp.setInput(i, null);
            xp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,false);
            event = xp.getEventType();

        }
        catch(Exception e)
        {

            e.printStackTrace();
        }

            while(event!=XmlPullParser.END_DOCUMENT){
              name=xp.getName();

                switch(event){

                    case XmlPullParser.START_TAG:
                        break;
                    case XmlPullParser.TEXT:
                            text=xp.getText();
                            break;
                    case XmlPullParser.END_TAG:

                        if(name.equals("country")){
                            country=text;
                            break;
                        }

                         else if(name.equals("temperature")){
                            temperature=xp.getAttributeValue(null,"value");
                            publishProgress(temperature);
                            break;
                            }
                        else
                        break;
                        }
                try {
                    event=xp.next();
                } catch (XmlPullParserException e) {

                        e.printStackTrace();
                } catch (IOException e) {

                    e.printStackTrace();
                }
            }

谁能帮我理解这里的问题..

【问题讨论】:

    标签: android xml android-asynctask xml-parsing


    【解决方案1】:

    如果你想获得属性,你应该在值所在的开始标记中进行。类似这样的东西:

    String mAttribute;
    //......
    
    case XmlPullParser.START_TAG:
    mAttribute = xp.getAttributeValue(null,"value");
        break;
    case XmlPullParser.TEXT:
            text=xp.getText();
            break;
    case XmlPullParser.END_TAG:
    
        if(name.equals("country")){
            country=text;
            break;
        }
    
         else if(name.equals("temperature")){
            temperature=mAttribute;
            publishProgress(temperature);
            break;
            }
        else
        break;
        }
    //.....
    

    【讨论】:

    • Thanks.this 解决了这个问题..但我很好奇为什么结束标签选项在你的情况下不起作用,我也将值存储在 mattribute 中,然后在结束标签中分配它..
    • 这个 就像 ,你可以看到结束标签没有任何属性
    猜你喜欢
    • 1970-01-01
    • 2016-11-05
    • 2021-11-17
    • 2011-07-26
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多