2

我有一个很大的 xml 文件(大约 100 多个项目),你如何根据String KEY_ID = "id";XML 中的“”过滤下载?下面是我的 XML 代码示例。例如,我想列出String KEY_ID = "id";从 1 到 20 的项目“”以显示在我的网格视图中。我的目标是限制 xmlparsing。目前我的代码只是下载我的 xml 中的所有内容并在 gridview 中显示它们。

我的XML.xml

<song>
<id>1</id>
<title>1</title>
<artist>Blabla</artist>
<duration>0</duration>
<thumb_url>https://jpg</thumb_url>
<big_url>https://jpg</big_url>
</song>

<song>
<id>2</id>
<title>2</title>
<artist>Nature</artist>
<duration>0</duration>
<thumb_url>https://jpg</thumb_url>
<big_url>https://jpg</big_url>
</song>

<song>
<id>3</id>
<title>3</title>
<artist>Nature</artist>
<duration>0</duration>
<thumb_url>https://jpg</thumb_url>
<big_url>https://jpg</big_url>
</song>
</music>

MainGridView.class

public class MainGridView extends Activity {

    private ProgressDialog pDialog;
    ArrayList<HashMap<String, String>> songsList;
    static final String KEY_SONG = "song"; 
    static final String KEY_ID = "id";
    static final String KEY_TITLE = "title";
    static final String KEY_ARTIST = "artist";
    static final String KEY_CAT_ARTIST = "artistcat";
    static final String KEY_DURATION = "duration";
    static final String KEY_THUMB_URL = "thumb_url";
    static final String KEY_BIG_URL = "big_url";
    static final String KEY_CAT_URL = "cat_url";
    static String IMAGE_POSITION;
    GridView grid;
    MainGridViewLazyAdapter adapter;
    String cat_url;
    String artist_url;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gridview_main);
        new loadGridView().execute();


        grid = (GridView) findViewById(R.id.grid_view);

  public class loadGridView extends AsyncTask<Integer, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainGridView.this);
            pDialog.setTitle("Connect to Server");
            pDialog.setMessage("This process can take a few seconds to a few minutes, depending on your Internet Connection Speed.");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        @Override
        protected String doInBackground(Integer... args) {
            // updating UI from Background Thread
                    Intent in = getIntent();
                    songsList = new ArrayList<HashMap<String, String>>();
                    cat_url = in.getStringExtra(KEY_CAT_URL);
                    artist_url = in.getStringExtra(KEY_CAT_ARTIST);


                    XMLParser parser = new XMLParser();
                    String xml = parser.getXmlFromUrl(cat_url); // getting XML from URL
                    Document doc = parser.getDomElement(xml); // getting DOM element

                    NodeList nl = doc.getElementsByTagName(KEY_SONG);
                    // looping through all song nodes <song>
                    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(KEY_ID, parser.getValue(e, KEY_ID));
                        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                        map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
                        map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                        map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
                        map.put(KEY_BIG_URL, parser.getValue(e, KEY_BIG_URL));
                        // adding HashList to ArrayList
                        songsList.add(map);

                    }
                    return null;
                }   


        @Override
        protected void onPostExecute(String args) {

            adapter=new MainGridViewLazyAdapter(MainGridView.this, songsList);   
            grid.setAdapter(adapter);
            pDialog.dismiss();
            }
        }
4

1 回答 1

0

XML 解析实际上通常非常快(在某些手机中比 JSON 更快)。但是要显示您可能想要查看 XPath (java.xml.xpath) 的一些文档 - 例如:

/music/song[id <= 20]

请参阅http://developer.android.com/reference/javax/xml/xpath/package-summary.html

于 2012-08-30T06:46:16.393 回答