如何从XML Pull Parser获取Url

时间:2013-01-17 08:55:14

标签: android xml http url xml-parsing

我无法从我收到的数据中提取网址。

数据

<ASX VERSION="3.0"><TITLE>iROCK109 - Classic Rock And More!</TITLE><MOREINFO HREF="http://www.irock109.com/" /><COPYRIGHT>� iROCK109 - 2009</COPYRIGHT><ABSTRACT>Classic Rock n More!</ABSTRACT><ENTRY><TITLE>[128k] www.irock109.com</TITLE><REF HREF="http://cp6.digistream.info:20366/" /><AUTHOR>iROCK 109 - Classic Rock And More!</AUTHOR><ABSTRACT>DJ Hosted Classic Rock 60s through today, and requests 24/7</ABSTRACT>

我正在使用的代码是从REF中读取数据和extrrat HREF

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            executeHttpGet();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
        //Method for http request

        public void executeHttpGet() throws Exception {

            BufferedReader in = null;

            try {

                System.out.println("ndkjsdak");
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet();

                HttpProtocolParams.setUseExpectContinue(client.getParams(), false);


                request.setURI(new URI("http://www.irock109.com/streams/irock109-128mp3.asx"));

                //HttpResponse response = client.execute(request);

                HttpResponse response = null;

                boolean RetryConnection = true;
                int retrycount = 0;

                //retry request 5 times
                while(RetryConnection == true && retrycount < 5)
                {
                    try
                    {
                         response = client.execute(request);
                         RetryConnection = false;
                    }
                    catch(IOException e)
                    {
                        System.out.println("caught");
                        retrycount += 1;
                    }
                }

                in = new BufferedReader
                (new InputStreamReader(response.getEntity().getContent()));

                StringBuffer sb = new StringBuffer("");
                String line = "";
                String NL = System.getProperty("line.separator");

                while ((line = in.readLine()) != null)
                {
                    sb.append(line +NL);
                }
                in.close();
                String page=sb.toString();
                System.out.println("xml data "+page);

                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                 factory.setNamespaceAware(true);
                 XmlPullParser xpp = factory.newPullParser();

                 xpp.setInput( new StringReader (page) );
                 int eventType = xpp.getEventType();
                 while (eventType != XmlPullParser.END_DOCUMENT)
                 {
                  if(eventType == XmlPullParser.START_DOCUMENT)
                  {
                      System.out.println("Start document");
                  }
                  else if(eventType == XmlPullParser.START_TAG)
                  {
                      System.out.println("Start tag "+xpp.getName());
                  }
                   else if(eventType == XmlPullParser.END_TAG)
                  {
                      System.out.println("End tag "+xpp.getName());
                  }
                  else if(eventType == XmlPullParser.TEXT) 
                  {
                      System.out.println("Text "+xpp.getText());
                  }
                  eventType = xpp.next();
                 }
                 System.out.println("End document");
            }
            finally 
            {
                if (in != null) 
                {
                    try
                    {
                        in.close();
                    } 
                    catch (IOException e)
                        {
                        e.printStackTrace();
                    }
                }
            }
    }
}

如何从href获取ref

1 个答案:

答案 0 :(得分:1)

试试这个

try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();

            xpp.setInput(new StringReader(xmlData));
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_DOCUMENT) {
                    System.out.println("Start document");
                } else if (eventType == XmlPullParser.START_TAG) {
                    if(xpp.getName().equalsIgnoreCase("REF"))
                    {
                        System.out.println("REF Text = " + xpp.getAttributeValue(0));
                    }
                    System.out.println("Start tag " + xpp.getName());
                } else if (eventType == XmlPullParser.END_TAG) {
                    System.out.println("End tag " + xpp.getName());
                } else if (eventType == XmlPullParser.TEXT) {
                    System.out.println("Text  = " + xpp.getText());

                }
                eventType = xpp.next();
            }
            System.out.println("End document");
        } catch (Exception e) {
        }