XmlPullParser does not parse self-closing tag, simply skip it, even "isEmptyElementTag" doesn't help. How to solve this problem? I want to parse self-closing tag company.
XML structure
<phone>
<id>1</id>
<company/> // it should parse
<model>Galaxy</model>
<price>18000</price>
</phone>
Code:
try {
XmlPullParser xpp = prepareXpp();
while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
switch (xpp.getEventType()) {
case XmlPullParser.START_TAG:
if (xpp.isEmptyElementTag()) {
tagName = "company"; }
break;
case XmlPullParser.TEXT:
if (tagName.equals("company")) {
Log.d(LOG_TAG, "Empty tag" );
}
break;
default:
break;
}
xpp.next();
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
XmlPullParser prepareXpp () {
return getResources().getXml(R.xml.data);
As you can see in documentation:
Empty element (such as <tag/>) will be reported with two separate events: START_TAG, END_TAG
So it does not enter case XmlPullParser.TEXT:
tag.
If you add some logging to START_TAG
you should see logs in console:
case XmlPullParser.START_TAG:
if (xpp.isEmptyElementTag()) {
Log.d(LOG_TAG, "Empty tag");
}
break;
Edit: You could try to do next workaround move your xml file to raw folder and add next code to :
XmlPullParser prepareXpp () {
InputStream istream = getResources().openRawResource(R.raw.data);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(istream, "UTF-8");
return xpp;
}