我目前正在创建一个Android应用程序,我有一个供用户选择的列表,我想要一侧的每个项目的图像。我为列表创建了一个适配器,并为列表创建了一个单独的list_row布局。
列表的实际数据存储在本地res / xml / data.xml中,如下所示:
<dataset>
<data>
<name>all</name>
<image>all</image>
</data>
<data>
<name>car</name>
<image>car</image>
</data>
</dataset>
现在,在此列表的主活动上,我尝试从本地源解析XML数据,然后将其作为字符串传递给Document元素并处理数据。我已经调试了这个问题,并注意到下面的行无效,因为正在设置的字符串为空:
XmlResourceParser parserXml = getResources().getXml(R.xml.data);
String xmlString = parserXml.getText();
解析数据的完整代码如下:
XmlResourceParser parserXml = getResources().getXml(R.xml.dresses);
String xmlString = parserXml.getText(); // getting XML from URL
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(xmlString); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_DATA);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_IMAGE, parser.getValue(e, KEY_IMAGE));
dataInfo.add(map);
}
任何人都可以帮助我,提前谢谢:)
EDIT .....
我也尝试过使用XmlPullParser方法作为建议,但我仍然遇到String为null的相同问题。我使用的代码如下:
XmlPullParser xpp = this.getResources().getXml(R.xml.data);
String xmlString1 = xpp.getText();
如果这有助于我将以下整个文件包含在内:
public class Dresses extends Activity {
static final String KEY_DATA = "dress"; // parent node
static final String KEY_NAME = "name";
static final String KEY_IMAGE = "image";
ListView list;
ItemTypeAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<HashMap<String, String>> itemTypes = new ArrayList<HashMap<String, String>>();
XmlPullParserFactory factory;
try {
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
} catch (XmlPullParserException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
XmlPullParser xpp = this.getResources().getXml(R.xml.data);
String xmlString1 = xpp.getText();
Log.d("Testing Type List Data: ", xmlString1);
XmlResourceParser parserXml = getResources().getXml(R.xml.data);
String xmlString = parserXml.getText(); // getting XML from URL
Log.d("Type List Data: ", xmlString);
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(xmlString); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_DATA);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_IMAGE, parser.getValue(e, KEY_IMAGE));
itemTypes.add(map);
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new ItemTypeAdapter(this, itemTypes);
list.setAdapter(adapter);
// listening to single list item on click
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
String type = ((TextView) view).getText().toString();
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), ItemListActivity.class);
// sending data to new activity
i.putExtra("type", type);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.data, menu);
return true;
}
}
答案 0 :(得分:1)
确保您在活动中调用此内容。如果它仍然不起作用,那么也尝试使用XmlPullParserFactory
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = context.getResources().getXml(R.xml.data);
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if(eventType == XmlPullParser.START_TAG) {
System.out.println("Start tag "+xpp.getName());
} else if(eventType == XmlPullParser.END_TAG) {
System.out.println("End tag "+xpp.getName());
} else if(eventType == XmlPullParser.TEXT) {
System.out.println("Text "+xpp.getText());
} else if(eventType == XmlPullParser.TEXT) {
}
eventType = xpp.next();
}
System.out.println("End document");
这对我有用
package com.example.test4;
import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
static final String KEY_DATA = "dress"; // parent node
static final String KEY_NAME = "name";
static final String KEY_IMAGE = "image";
ListView list;
// ItemTypeAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
XmlPullParserFactory factory = null;
try {
factory = XmlPullParserFactory.newInstance();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
factory.setNamespaceAware(true);
XmlPullParser xpp = getResources().getXml(R.xml.data);
int eventType = 0;
try {
eventType = xpp.getEventType();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if (eventType == XmlPullParser.START_TAG) {
System.out.println("Start tag " + xpp.getName());
} else if (eventType == XmlPullParser.END_TAG) {
System.out.println("End tag " + xpp.getName());
} else if (eventType == XmlPullParser.TEXT) {
System.out.println("Text " + xpp.getText());
} else if (eventType == XmlPullParser.TEXT) {
}
try {
eventType = xpp.next();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("End document");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
这是输出
04-05 14:37:08.690: I/System.out(27153): Start document
04-05 14:38:48.185: I/System.out(27153): Start document
04-05 14:39:30.795: I/System.out(27153): Start tag dataset
04-05 14:40:09.540: I/System.out(27153): Start tag data
04-05 14:40:09.540: I/System.out(27153): Start tag name
04-05 14:40:09.540: I/System.out(27153): Text all
04-05 14:40:09.540: I/System.out(27153): End tag name
04-05 14:40:09.540: I/System.out(27153): Start tag image
04-05 14:40:09.540: I/System.out(27153): Text all
04-05 14:40:09.540: I/System.out(27153): End tag image
04-05 14:40:09.540: I/System.out(27153): End tag data
04-05 14:40:09.540: I/System.out(27153): Start tag data
04-05 14:40:09.545: I/System.out(27153): Start tag name
04-05 14:40:09.545: I/System.out(27153): Text car
04-05 14:40:09.550: I/System.out(27153): End tag name
04-05 14:40:09.550: I/System.out(27153): Start tag image
04-05 14:40:09.550: I/System.out(27153): Text car
04-05 14:40:09.550: I/System.out(27153): End tag image
04-05 14:40:09.550: I/System.out(27153): End tag data
04-05 14:40:09.550: I/System.out(27153): End tag dataset
04-05 14:40:09.555: I/System.out(27153): End document
res / xml / data.xml中的Data.xml
<?xml version="1.0" encoding="utf-8"?>
<dataset>
<data>
<name>all</name>
<image>all</image>
</data>
<data>
<name>car</name>
<image>car</image>
</data>
</dataset>