【问题标题】:parse Xml data like json parse像 json 解析一样解析 XML 数据
【发布时间】:2015-02-02 15:28:08
【问题描述】:

我正在开发 android 应用程序,当我解析像 Json 这样的 XML 数据时出现错误。我很困惑有多少数组有这些数据?我在下面分享代码。任何人都可以帮助我或分享像这个 xml 数据这样的示例代码吗?

public class MainActivity extends Activity implements OnRefreshListener,
        OnClickListener {
    // Declare Variables
    JSONObject jsonobject;
    JSONArray jsonarray;
    JSONArray jsonarray2;
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    static String RANK = "rank";
    static String COUNTRY = "country";
    static String POPULATION = "population";
    static String FLAG = "flag";

    Button btngeri;

    static Integer[] imageId = { R.drawable.enveloppe, R.drawable.enveloppe,
            R.drawable.enveloppe, R.drawable.enveloppe, R.drawable.enveloppe,
            R.drawable.enveloppe, R.drawable.enveloppe };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from listview_main.xml
        setContentView(R.layout.listview_main);

        btngeri = (Button) findViewById(R.id.btn_geri);
        btngeri.setOnClickListener(this);


        // Execute DownloadJSON AsyncTask
        new DownloadJSON().execute();
    }

    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(MainActivity.this);
            // Set progressdialog title
            mProgressDialog.setTitle("Mail Kutusu");
            // Set progressdialog message
            mProgressDialog.setMessage("Güncelleme Yapılıyor...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonobject = JSONfunctions
                    .getJSONfromURL("http://78.186.62.169:8210/AnketServis.asmx/Message");

            try {
                // Locate the array name in JSON

                jsonarray = jsonobject.getJSONArray("MessageVO");

                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                    // map.put("rank", jsonobject.getString("rank"));
                    map.put("country", jsonobject.getString("Konu"));
                    // map.put("population",
                    // jsonobject.getString("population"));
                    // map.put("flag", jsonobject.getString("flag"));
                    // Set the JSON Objects into the array
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(MainActivity.this, arraylist, imageId);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }

【问题讨论】:

  • 如果您裁剪该图像以更清晰地显示数据会有所帮助。

标签: android xml json


【解决方案1】:

你不能像 json 那样解析 xml 文件。您必须使用 xml 解析器。有两种解析方法。 SAX 和 Dom。哟可以看到他们的对比。 What is the difference between SAX and DOM? 你可以在android中看到一个xml解析器示例。

http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2014-06-02
    • 2015-12-08
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    相关资源
    最近更新 更多