XML プル パーサーを使用して xml ファイルを解析しようとすると、例外が発生します。例外は XmlPullParserException: Unexpected Token( position: TEXT @ 1:2 in java.io.stringreader@40e06970 と言っています 解決策を教えてください、私のコードは、
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;
}
}
国.xml
<CountriesList>
<countries>
<country>
Afghanistan
</country>
</countries>
<countries>
<country>
Albania
</country>
</countries>
<countries>
<country>
Algeria
</country>
</countries>
<countries>
</CountriesList>