我有一个大的xml文件(大约100多个项目),你如何根据XML中的“String KEY_ID = "id";
”过滤下载?下面是我的XML代码示例。例如,我想列出1到20之间的项目“String KEY_ID = "id";
”,以便在我的gridview中显示。我的目标是限制xmlparsing。目前我的代码只是下载我的xml中的所有内容并在gridview中显示它们。
myXML.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();
}
}
答案 0 :(得分:0)
XML解析实际上通常非常快(在某些手机中比JSON快)。但是要显示一些您可能想要查看XPath(java.xml.xpath)的文档 - 例如:
/music/song[id <= 20]
请参阅http://developer.android.com/reference/javax/xml/xpath/package-summary.html