Share via


IXmlPullParser.NextText Method

Definition

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.

[Android.Runtime.Register("nextText", "()Ljava/lang/String;", "GetNextTextHandler:Org.XmlPull.V1.IXmlPullParserInvoker, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")]
public string? NextText ();
[<Android.Runtime.Register("nextText", "()Ljava/lang/String;", "GetNextTextHandler:Org.XmlPull.V1.IXmlPullParserInvoker, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")>]
abstract member NextText : unit -> string

Returns

Attributes

Exceptions

Remarks

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: <ol> <li>&lt;tag&gt;foo&lt;/tag&gt; <li>&lt;tag&gt;&lt;/tag&gt; (which is equivalent to &lt;tag/&gt; 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(getEventType() != START_TAG) {
                throw new XmlPullParserException(
                  "parser must be on START_TAG to read next text", this, null);
             }
             int 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);
             }

<strong>Warning:</strong> Prior to API level 14, the pull parser returned by android.util.Xml did not always advance to the END_TAG event when this method was called. Work around by using manually advancing after calls to nextText():

String text = xpp.nextText();
             if (xpp.getEventType() != XmlPullParser.END_TAG) {
                 xpp.next();
             }

Java documentation for org.xmlpull.v1.XmlPullParser.nextText().

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to