Reputation: 2403
I try to parse simple xml file with XmlPullParser.
<?xml version="1.0" encoding="utf-8"?>
<tests>
<test>
<name>Jack</name>
</test>
<test>
<name>Brad</name>
</test>
<test>
<name>Tom</name>
</test>
</tests>
This is the MainActivity code:
public class MainActivity extends ActionBarActivity {
ViewPager viewPager;
PagerAdapter pagerAdapter;
ArrayList<Quote> quotes;
ArrayList<Pers> persons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InputStream inputStream = getResources().openRawResource(R.xml.test);
Fetcher fetcher = new Fetcher();
persons = fetcher.parse(inputStream);
String str = new String();
for(int i = 0; i < persons.size(); i++) {
str += persons.get(i).getName();
}
Log.d("1", str);
}
}
This is the Fetcher class code:
public class Fetcher {
private ArrayList<Pers> persons;
private Pers person;
private String text;
public Fetcher() {
persons = new ArrayList<Pers>();
}
public ArrayList<Pers> parse(InputStream is) {
XmlPullParserFactory factory = null;
XmlPullParser parser = null;
try {
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
parser = factory.newPullParser();
parser.setInput(is, null);
int eventType = parser.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT) {
String tagname = parser.getName();
switch(eventType) {
case XmlPullParser.START_TAG:
if(tagname.equalsIgnoreCase("test")) {
person = new Pers();
}
break;
case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if(tagname.equalsIgnoreCase("test")) {
persons.add(person);
} else if(tagname.equalsIgnoreCase("name")) {
person.setName(text);
}
break;
default:
break;
}
eventType = parser.next();
}
} catch(XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return persons;
}
}
Why does XmlPullParserException exception occurs?
org.xmlpull.v1.XmlPullParserException: unterminated entity ref (position:TEXT ��������������������...@1:49 in java.io.InputStreamReader@405534d8)
Upvotes: 0
Views: 2564
Reputation: 914
A bit late but, in case it helps anyone else, check:
I checked through my XML by starting with the outermost tags, deleting the content and re-adding in bits, running each time, to find where the errors were. Debugging XML files is a real pain!
Upvotes: 0
Reputation: 2403
I changed code, now it works. Thank those who tried to help me.
public class Fetcher {
private ArrayList<Pers> persons;
private Pers person;
private String text;
public Fetcher() {
persons = new ArrayList<Pers>();
}
public ArrayList<Quote> parse(Activity activity) throws XmlPullParserException, IOException {
Resources resources = activity.getResources();
XmlResourceParser xmlResourceParser = resources.getXml(R.xml.quotes);
person = new Pers();
xmlResourceParser.next();
int eventType = xmlResourceParser.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT) {
String tagName = xmlResourceParser.getName();
switch(eventType) {
case XmlPullParser.START_TAG: {
if(tagName.equalsIgnoreCase("test")) {
person = new Pers();
}
break;
}
case XmlPullParser.TEXT: {
text = xmlResourceParser.getText();
break;
}
case XmlPullParser.END_TAG: {
if(tagName.equalsIgnoreCase("test")) {
persons.add(person);
} else if(tagName.equalsIgnoreCase("name")) {
person.setId(Integer.parseInt(text));
}
break;
}
default:
break;
}
eventType = xmlResourceParser.next();
}
return persons;
}
}
Upvotes: 2
Reputation: 718886
I think that the problem is in different input to what you have shown us. Or maybe the source code is different.
The error message seems to refer to a position that does not exist - line 1, character position 49 (or vice versa).
The error message seems to indicate some garbled characters (or a string of NULs), but there is nothing problematic in the sample XML.
Upvotes: 0
Reputation: 1469
I imagine it's this line: parser.setInput(is, null);
Because the xml encoding is UTF-8, I believe you have to change it to parser.setInput(is,"UTF-8");
Upvotes: 0