像json parse一样解析Xml数据

时间:2015-02-02 15:28:08

标签: android xml json

我正在开发android应用程序而且我得到了一个错误当我解析像Json这样的XML数据时。我混淆了有多少数组有这些数据?我在下面分享代码。任何人都可以帮助我或分享像这个xml数据的示例代码? I added photo of parsing data

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();
        }
    }

1 个答案:

答案 0 :(得分:0)

哟无法像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/