1

如何从以下 xml 解析和检索数据?

<?xml version="1.0" encoding="iso-8859-7"?>
<PRODUCT id='107230' name='WESTERN DIGITAL 2TB WD20EZRX [SATA 3 - 64MB]' price='89.00' photo='http://www.priveshop.gr/products/WD20EZRX_A.jpg'></PRODUCT>
    public class MainActivity extends Activity {
    // Declare Variables
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
     static String PRODUCT = "PRODUCT";
    static String RANK = "id";
    static String COUNTRY = "name";
    static String POPULATION = "price";
    //static String FLAG = "photo";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from listview_main.xml
        setContentView(R.layout.listview_main);
        // Execute DownloadJSON AsyncTask
        new DownloadXML().execute();
    }

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

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(MainActivity.this);
            // Set progressdialog title
            mProgressDialog.setTitle("XtronLabs");
            // Set progressdialog message
            mProgressDialog.setMessage(" xtron searchin your web.............wait.");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create the array
            arraylist = new ArrayList<HashMap<String, String>>();

            XMLParser parser = new XMLParser();
            // Retrive nodes from the given website URL in XMLParser.class

            String xml = parser.getXmlFromUrl("http://www.priveshop.gr/exte/android/xml_listing.php?key=12121212&id=1072");
            // Retrive DOM element
            Document doc = parser.getDomElement(xml);



            NodeList nl = doc.getElementsByTagName(PRODUCT);

        //  try {
                // Locate the NodeList name

            // Toast.makeText(MainActivity.this,PRODUCT , Toast.LENGTH_SHORT).show();
                for (int i = 0; i < nl.getLength(); i++) {
                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();
                    Element e = (Element) nl.item(i);
                    // adding each child node to HashMap key => value
                    //map.put(PRODUCT, parser.getValue(e, PRODUCT));
                    map.put(RANK, parser.getValue(e, RANK));
                    map.put(COUNTRY, parser.getValue(e, COUNTRY));
                    map.put(POPULATION, parser.getValue(e, POPULATION));
                //  map.put(FLAG, parser.getValue(e, FLAG));
                    // adding HashList to ArrayList
                    arraylist.add(map);


                }
        //  } catch (Exception e) {
            //  Log.e("Error", e.getMessage());
                //e.printStackTrace();
        //  }

            // Toast.makeText(MainActivity.this, "button" +" "+ PRODUCT, Toast.LENGTH_SHORT).show();
            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);
            // Binds the Adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }
}

我们只能从 XML 而不是实际数据中获取长度...谁能告诉我出了什么问题?

4

0 回答 0