从xml获取值

时间:2018-05-09 11:22:46

标签: android xml-parsing

我有来自网址xml

的xml

我将xml发送到我的Android应用程序。但我不知道如何从xml获取指定的值。我需要在内部获得地理位置" ReportBody"其中index = 1。我在下面附上我的java代码。提前感谢任何积极的帮助。

public class MainActivity extends AppCompatActivity {
String xml;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new Connection().execute();
}
private class Connection extends AsyncTask {

    @Override
    protected String doInBackground(Object... arg0) {
        try {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet("http://server5.onlinegpstracker.com/xml_files/2501_group_summary.xml");
            HttpResponse response = client.execute(request);
            HttpEntity httpEntity = response.getEntity();
            xml = EntityUtils.toString(httpEntity);
        } catch (ClientProtocolException e) {
            Log.d("HTTPCLIENT", e.getLocalizedMessage());
        } catch (IOException e) {
            Log.d("HTTPCLIENT", e.getLocalizedMessage());
        }
        return xml;
    }
}
}

1 个答案:

答案 0 :(得分:0)

当您从url获取xml作为字符串时,应该借助xml解析器库将其解析为java对象,您可以执行此操作以获取更多信息,请访问此链接:

https://developer.android.com/training/basics/network-ops/xml