我已经编写了一个程序来从Eclipse中的XML文件中获取数据,并且我已经打印到文本视图以确保尝试正常,但它没有传递数据,但我不确定我是否得到了错误的参数,或者我称它为错误,所以任何帮助都会非常感激。
public class AnimalList extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animal_list);
//pulls data from XML file
XmlPullParserFactory pullParserFactory;
try
{
pullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = pullParserFactory.newPullParser();
InputStream in_s = getApplicationContext().getAssets().open("animals.xml");
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in_s, null);
parseXML(parser);
TextView display = (TextView)findViewById(R.id.animalInfo);
display.setText("crap");
}
catch (XmlPullParserException e)
{
e.printStackTrace();
}
catch (IOException e)
{
//Auto-generated catch block
e.printStackTrace();
}
}
我知道上面的工作是通过打印到文本视图,但接下来我有
private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
{
ArrayList<Animal> animals = new ArrayList<Animal>();
int eventType = parser.getEventType();
Animal currentAnimal = null;
while (eventType != XmlPullParser.END_DOCUMENT)
{
String name = null;
switch (eventType)
{
case XmlPullParser.START_DOCUMENT:
//animals = new ArrayList();
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name == "animal")
{
currentAnimal = new Animal();
}
else if (currentAnimal != null)
{
if (name == "specificLocation")
{
currentAnimal.specificLocation = parser.nextText();
}
else if (name == "name"){
currentAnimal.name = parser.nextText();
}
else if (name == "location")
{
currentAnimal.location= parser.nextText();
}
else if (name == "image")
{
currentAnimal.image= parser.nextText();
}
else if (name == "animalInfo")
{
currentAnimal.animalInfo= parser.nextText();
}
else if (name == "todaysFeed")
{
currentAnimal.todaysFeed= parser.nextText();
}
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("animal") && currentAnimal != null){
animals.add(currentAnimal);
}
}
eventType = parser.next();
}
printAnimals(animals);
}
然后其中的代码没有做任何事情,所以希望有人能指出我是否正确传递了它?在第二个内部,它只是将数据放入一个数组中,但如果我在进行任何操作之前输入文本视图输出,它就没有做任何让我觉得它没有传递数据的事情
感谢。
答案 0 :(得分:0)
搜索标记名称您已比较引用,但不比较字符串。它的工作正常。所以parseXML就是这样的:
private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
{
ArrayList<Animal> animals = new ArrayList<Animal>();
int eventType = parser.getEventType();
Animal currentAnimal = null;
while (eventType != XmlPullParser.END_DOCUMENT)
{
String name = null;
switch (eventType)
{
case XmlPullParser.START_DOCUMENT:
//animals = new ArrayList();
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equalsIgnoreCase( "animal"))
{
currentAnimal = new Animal();
}
else if (currentAnimal != null)
{
if (name.equalsIgnoreCase( "specificLocation"))
{
currentAnimal.specificLocation = parser.nextText();
}
else if (name .equalsIgnoreCase( "name")){
currentAnimal.name = parser.nextText();
}
else if (name .equalsIgnoreCase( "location"))
{
currentAnimal.location= parser.nextText();
}
else if (name .equalsIgnoreCase( "image"))
{
currentAnimal.image= parser.nextText();
}
else if (name .equalsIgnoreCase( "animalInfo"))
{
currentAnimal.animalInfo= parser.nextText();
}
else if (name .equalsIgnoreCase( "todaysFeed"))
{
currentAnimal.todaysFeed= parser.nextText();
}
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("animal") && currentAnimal != null){
animals.add(currentAnimal);
}
}
eventType = parser.next();
}
printAnimals(animals);
}
希望它有帮助:) 马尔钦