Question

I serialize array of strings to XML, my output:

<MyArray>
    <anyType xsi:type="xsd:string">testString</anyType>
    <anyType xsi:type="xsd:string"></anyType> // here is empty string, and I want it in output array
</MyArray>

When I try to deserialize this back to array with XMLPullParser it's create array only with one string. Why function next() from XmlPullParser on second string only return START_TAG and END_TAG, without TEXT event? There is any workaround to get empty string from this xml?

Was it helpful?

Solution

According to http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html, more specifically the nextText() method documentation, you get:

If current event is START_TAG then if next element is TEXT then element content is returned or if next event is END_TAG then empty string is returned, otherwise exception is thrown. After calling this function successfully parser will be positioned on END_TAG. The motivation for this function is to allow to parse consistently both empty elements and elements that has non empty content.

The sample code they provide works well, and I'm currently using in my code something in the lines of:

mPullParser.nextTag();
mPullParser.require(XmlPullParser.START_TAG, null, tag);
final String text = mPullParser.nextText();
mPullParser.require(XmlPullParser.END_TAG, null, tag);

Which works just fine, regardless it's an empty string or not. Are you using the org.xmlpull.v1.XmlPullParser library?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top