【问题标题】:Getting a value from xml从 xml 中获取值
【发布时间】:2018-05-09 11:22:45
【问题描述】:

我有一个来自 url xml 的 xml

我将 xml 获取到我的 android 应用程序。但我不知道如何从 xml 中获取指定的值。我需要在 index=1 的“ReportBody”中获取地理点。我在下面附上了我的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;
    }
}
}

【问题讨论】:

标签: android xml-parsing


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 2017-08-15
    • 2016-11-05
    • 2018-10-27
    • 2012-07-24
    相关资源
    最近更新 更多