Android通过url从xml标签获取特定值



我正试图通过url从XML文件中获取特定标记中包含的数据。特定标签称为start_address。我不确定在这种情况下该用什么方法。这是我连接到的确切XML文件的链接

http://maps.googleapis.com/maps/api/directions/xml?origin=07306&目的地=07306&传感器=错误

//主要活动

    package com.example.xmlread;
    import java.io.IOException;
    import java.net.URL;
    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserException;
    import org.xmlpull.v1.XmlPullParserFactory;
    import android.support.v7.app.ActionBarActivity;
    import android.content.res.XmlResourceParser;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    public class MainActivity extends ActionBarActivity {
        public static Button GetDir, Clear;
        public static EditText StartDes, EndDes;
        public static String TAG = "GoogleDirectionAPI";
        //public static String STARTING_DESTINATION = StartDes.getText().toString();
        //public static String ENDING_DESTINATION = EndDes.getText().toString();
        //public static final String QUERY_URL = "http://maps.googleapis.com/maps/api/directions/xml?origin=" + STARTING_DESTINATION + "&destination=" + ENDING_DESTINATION + "&sensor=false";
        public static final String TEST_URL = "http://maps.googleapis.com/maps/api/directions/xml?origin=07306&destination=07306&sensor=false";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
        public void ClearDisplay(View v){
        }
        public void GetDirections(View v){
            Log.i(TAG, "Query Server..");
            AsyncDownloader downloader = new AsyncDownloader();
            downloader.execute();
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        //background downloading for XML data
        private class AsyncDownloader extends AsyncTask<Object, String, Integer>{
            @Override
            protected Integer doInBackground(Object... arg0){
                XmlPullParser receivedData = tryDownloadingXmlData();
                int RecordsFound = tryParsingXmlData(receivedData);
                return RecordsFound;
            }
            private XmlPullParser tryDownloadingXmlData() {
                try{
                    URL xmlUrl = new URL(TEST_URL);
                    XmlPullParser recieveData = XmlPullParserFactory.newInstance().newPullParser();
                    recieveData.setInput(xmlUrl.openStream(), null);
                    return recieveData;
                }catch (XmlPullParserException e){
                    Log.e(TAG,"XmlPullParserException", e);
                }catch (IOException e){
                    Log.e(TAG, "XmlPullParserException", e);
                }
                return null;
            }
            @Override
            protected void onProgressUpdate(String... values){
                if(values.length == 0){
                    Log.i(TAG, "No data downloaded");
                }
                if(values.length == 1){
                    String start_address = values[0];
                    //String end_address = values[1];
                    //String html_instructions = values[2];
                    //log
                    Log.i(TAG, "Start Address: " + start_address);
                    //Log.i(TAG, "Ending Address: "+ end_address);
                    //Log.i(TAG, "html instructions: " + html_instructions);
                }
                super.onProgressUpdate(values);
            }
            private int tryParsingXmlData(XmlPullParser receivedData) {
                if(receivedData != null){
                    return processRecievedData(receivedData);
                    }
                return 0;
            }
            private int processRecievedData(XmlPullParser receivedData) {
                // TODO Auto-generated method stub
                int eventType = -1;
                int recordsFound = 0;
                //find values in XML records
                String start_address = "";
                //String end_address = "";
                //String html_instructions = "";
                while(eventType != XmlResourceParser.END_DOCUMENT){
                    String tagName = receivedData.getName();
//problem starts here!! 
                    switch(eventType){
                    case XmlResourceParser.START_TAG:
                        if(tagName.equals("start_address")){
                            //pull value
                            start_address = receivedData.getText(); // get text contained inside the start_address tag (not working and returns null)
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if(tagName.equals("start_address")){
                            recordsFound++;
                            publishProgress(start_address);
                        }
                        break;
                    }
                    try {
                        eventType = receivedData.next();
                    } catch (XmlPullParserException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                //if no data is found, publish empty event
                if(recordsFound == 0){
                    publishProgress();
                }
                Log.i(TAG, "Processed: "+ recordsFound + " Records");
                return recordsFound;
            }
        }
    }

使用下面的.nextText().trim()完整代码计算出来

        switch(eventType){
        case XmlPullParser.START_TAG:
            if(tagName.equals("start_address")){
                    try {
                        start_address = receivedData.nextText().trim();
                    } catch (XmlPullParserException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    recordsFound++;
                    publishProgress(start_address);
            }

相关内容

  • 没有找到相关文章

最新更新