XMLPullParser方法不起作用

时间:2014-01-08 04:26:46

标签: java android xml-parsing xmlpullparser

我正在尝试从此网址解析xml

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quote%20where%20symbol%20in%20(%22MSFT%22)&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys

并且在使用方法getName()时,它会提供null值。 下面是线程中的代码,请您告诉我错误的位置。

             @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        String urlString = arg0[0];
        String text = null;
        InputStream is = null;
        //String tagName = null;
        int count = 0;
        try {

            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
            connection.setReadTimeout(10*1000);
            connection.setConnectTimeout(10*1000);
            connection.setRequestMethod("GET");
            connection.setDoInput(true);
            connection.connect();

            int response = connection.getResponseCode();
            Log.d("debug", "the response is"+response);
            is =  new BufferedInputStream(connection.getInputStream());

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);

            XmlPullParser xpp = factory.newPullParser();
            xpp.setInput(is, null);

            int eventType = xpp.getEventType();
            Log.d("kchayar", is+"");
            while(eventType != XmlPullParser.END_DOCUMENT){

            String  tagName = xpp.getName();


                    if( eventType == XmlPullParser.START_TAG ){
                    //  if( tagName.equals("Change")){
                        //  text = xpp.nextText();
                        count ++;


                    //  }
                    }
                    if( eventType == XmlPullParser.TEXT ){
                    //  if( tagName.equals("Change")){
                            text = xpp.nextText();
                        count ++;


                        //}
                    }


                eventType = xpp.next(); 


            }




        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null; 

    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        wv.setText(result);
    }



}

1 个答案:

答案 0 :(得分:0)

String  tagName = xpp.getName();

我认为这一行不应该在

之内
if(eventType==XmlPullParser.START_TAG)

此处示例:http://www.vogella.com/tutorials/AndroidXML/article.html