TextView 不显示 xml 文件中的数据
我正在尝试从 xml 文件中检索一些数据。下面是应该可以工作的代码,但在本例中却不能。我没有收到错误,但数据没有显示。请查看此代码并告诉我这里出了什么问题。 (我正在尝试查看一个 id,如果它与某个数字匹配而不是显示文本) 谢谢。
String stringXmlContent;
try {
stringXmlContent = getEventsFromAnXML(this);
tv.setText(stringXmlContent);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
button1.setOnClickListener(this);
}
private String getEventsFromAnXML(Activity activity)throws XmlPullParserException, IOException
{
StringBuffer stringBuffer = new StringBuffer();
String attVal = null;
String desc;
Resources res = activity.getResources();
XmlResourceParser xrp = res.getXml(R.xml.myxml);
try {
xrp.next();
int eventType = xrp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if(eventType == XmlPullParser.START_DOCUMENT)
{
stringBuffer.append(" ");
}
if(eventType == XmlPullParser.START_TAG)
{
if(xrp.getName().equals("Number")){
attVal = xrp.getAttributeValue(0);
}
}
else if(eventType == XmlPullParser.TEXT)
{
if(xrp.getName().equals("Description") && attVal.equals("2")){
stringBuffer.append(" " + xrp.getText());
}
}
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stringBuffer.toString();
I am trying to retrieve some data from my xml file. Below is the code which should be working but in this case it doesn't. I am not getting errors but data just doesn't gets displayed.Please review this code and tell me what's wrong here.
(I am trying to look at an id, if it matches a certain digit than display text)
Thanks.
String stringXmlContent;
try {
stringXmlContent = getEventsFromAnXML(this);
tv.setText(stringXmlContent);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
button1.setOnClickListener(this);
}
private String getEventsFromAnXML(Activity activity)throws XmlPullParserException, IOException
{
StringBuffer stringBuffer = new StringBuffer();
String attVal = null;
String desc;
Resources res = activity.getResources();
XmlResourceParser xrp = res.getXml(R.xml.myxml);
try {
xrp.next();
int eventType = xrp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if(eventType == XmlPullParser.START_DOCUMENT)
{
stringBuffer.append(" ");
}
if(eventType == XmlPullParser.START_TAG)
{
if(xrp.getName().equals("Number")){
attVal = xrp.getAttributeValue(0);
}
}
else if(eventType == XmlPullParser.TEXT)
{
if(xrp.getName().equals("Description") && attVal.equals("2")){
stringBuffer.append(" " + xrp.getText());
}
}
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stringBuffer.toString();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论