gpt4 book ai didi

android - XmlResourceParser 为空?

转载 作者:行者123 更新时间:2023-11-30 03:40:40 25 4
gpt4 key购买 nike

我目前正在创建一个 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>

现在,在这个列表的主要 Activity 上,我尝试从本地源解析 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);
}

任何人都可以帮我解决这个问题,在此先感谢:)

编辑.....

我也尝试过按照建议使用 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;
}

}

最佳答案

确保您在 Activity 中调用它。如果它仍然不起作用,那么也尝试使用 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>

关于android - XmlResourceParser 为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15796310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com