nextText

open fun nextText(): String

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, for example for input:

  1. foo

  2. (which is equivalent to both input can be parsed with the same code:

p.nextTag()
p.requireEvent(p.START_TAG, "", "tag");
String content = p.nextText();
p.requireEvent(p.END_TAG, "", "tag");

This function together with nextTag make it very easy to parse XML that has no mixed content.

Essentially it does this

if(eventType != START_TAG) {
throw new XmlPullParserException(
"parser must be on START_TAG to read next text", this, null);
}
var eventType = next();
if (eventType == TEXT) {
String result = getText();
eventType = next();
if (eventType != END_TAG) {
throw new XmlPullParserException(
"event TEXT it must be immediately followed by END_TAG", this, null);
}
return result;
} else if (eventType == END_TAG) {
return "";
} else {
throw new XmlPullParserException(
"parser must be on START_TAG or TEXT to read text", this, null);
}