当我尝试使用XML Pull解析器解析xml文件时,我收到异常。异常说XmlPullParserException:意外的令牌(位置:java.io.stringreader@40e06970中的TEXT @ 1:2) 请给我一个解决方案,我的代码是,
MainAvtivity.java
package com.example.simplexmlpullapp;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
ListView list;
String str;
ArrayList<Data> arrdata;
ArrayList<String> arrayofString;
String textforinput = "<foo>Hello World!</foo>";
String parsedData = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
readXML();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void readXML() throws XmlPullParserException {
try {
Log.i("String","in readXML");
InputStream is = getAssets().open("countries.xml");
BufferedReader br=new BufferedReader(new InputStreamReader(is));
StringBuilder total=new StringBuilder();
String line;
while((line=br.readLine())!=null)
{
total.append(line);
}
Log.i("String","going in xml parsing"+total);
xmlParse(total.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void xmlParse(String file) throws XmlPullParserException, IOException
{
Log.i("String","going in xml parse");
boolean demoflag=false;
Data d=null;
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(file));
int eventType = xpp.getEventType();
while (eventType!=XmlPullParser.END_DOCUMENT) {
Log.i("String","in while");
switch(eventType)
{
case XmlPullParser.START_DOCUMENT:
break;
//System.out.println("Start document");
case XmlPullParser.START_TAG:
if(xpp.getName().equalsIgnoreCase("country"))
{
demoflag=true;
d=new Data();
}
break;
case XmlPullParser.TEXT:
if(demoflag)
{
d.setCountry(xpp.getText().trim());
Log.i("country", ""+xpp.getText().trim());
}
break;
case XmlPullParser.END_TAG:
if(xpp.getName().equalsIgnoreCase("country"))
{
arrdata.add(d);
demoflag=false;
}
break;
default :
break;
}
eventType=xpp.next();
}
arrayofString=new ArrayList<String>();
for(int i=0;i<arrdata.size();i++)
{
Data data=arrdata.get(i);
arrayofString.add(data.getCountry());
Log.i("parse_example", "country"+data.getCountry());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
country.xml
<CountriesList>
<countries>
<country>
Afghanistan
</country>
</countries>
<countries>
<country>
Albania
</country>
</countries>
<countries>
<country>
Algeria
</country>
</countries>
<countries>
</CountriesList>
答案 0 :(得分:1)
国家/地区列表标记未结束
</CountriesList>
你可以在这里验证xml
试试这个xml
<CountriesList>
<countries>
<country>
Afghanistan
</country>
</countries>
<countries>
<country>
Albania
</country>
</countries>
<countries>
<country>
Algeria
</country>
</countries>
</CountriesList>
UPDATE这将以logcat
打印国家/地区public void xmlParse(String file) throws XmlPullParserException, IOException {
Log.i("String", "going in xml parse");
boolean demoflag = false;
Data d = null;
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(file));
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
Log.i("String", "in while");
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
if (xpp.getName().equalsIgnoreCase("country")) {
demoflag = true;
}
break;
case XmlPullParser.TEXT:
if (demoflag) {
Log.i("country", "" + xpp.getText().trim());
}
break;
case XmlPullParser.END_TAG:
if (xpp.getName().equalsIgnoreCase("country")) {
demoflag = false;
}
break;
default:
break;
}
eventType = xpp.next();
}
}