diff options
-rw-r--r-- | luni/src/test/java/libcore/xml/PullParserTest.java | 22 | ||||
-rw-r--r-- | xml/src/main/java/org/xmlpull/v1/XmlPullParser.java | 11 |
2 files changed, 32 insertions, 1 deletions
diff --git a/luni/src/test/java/libcore/xml/PullParserTest.java b/luni/src/test/java/libcore/xml/PullParserTest.java index fa00fe5..71125aa 100644 --- a/luni/src/test/java/libcore/xml/PullParserTest.java +++ b/luni/src/test/java/libcore/xml/PullParserTest.java @@ -720,6 +720,28 @@ public abstract class PullParserTest extends TestCase { assertParseFailure("\ufeff<?xml version='1.0'?><input/>"); } + // http://code.google.com/p/android/issues/detail?id=21425 + public void testNextTextAdvancesToEndTag() throws Exception { + XmlPullParser parser = newPullParser(); + parser.setInput(new StringReader("<foo>bar</foo>")); + assertEquals(XmlPullParser.START_TAG, parser.next()); + assertEquals("bar", parser.nextText()); + assertEquals(XmlPullParser.END_TAG, parser.getEventType()); + } + + public void testNextTag() throws Exception { + XmlPullParser parser = newPullParser(); + parser.setInput(new StringReader("<foo> <bar></bar> </foo>")); + assertEquals(XmlPullParser.START_TAG, parser.nextTag()); + assertEquals("foo", parser.getName()); + assertEquals(XmlPullParser.START_TAG, parser.nextTag()); + assertEquals("bar", parser.getName()); + assertEquals(XmlPullParser.END_TAG, parser.nextTag()); + assertEquals("bar", parser.getName()); + assertEquals(XmlPullParser.END_TAG, parser.nextTag()); + assertEquals("foo", parser.getName()); + } + private void assertParseFailure(String xml) throws Exception { XmlPullParser parser = newPullParser(); parser.setInput(new StringReader(xml)); diff --git a/xml/src/main/java/org/xmlpull/v1/XmlPullParser.java b/xml/src/main/java/org/xmlpull/v1/XmlPullParser.java index 36e6025..308c006 100644 --- a/xml/src/main/java/org/xmlpull/v1/XmlPullParser.java +++ b/xml/src/main/java/org/xmlpull/v1/XmlPullParser.java @@ -1051,7 +1051,7 @@ public interface XmlPullParser { * 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. - * + * * <p>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><tag>foo</tag> @@ -1089,6 +1089,15 @@ public interface XmlPullParser { * "parser must be on START_TAG or TEXT to read text", this, null); * } * </pre> + * + * <p><strong>Warning:</strong> Prior to API level 14, the pull parser returned by {@code + * 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(): <pre> + * String text = xpp.nextText(); + * if (xpp.getEventType() != XmlPullParser.END_TAG) { + * xpp.next(); + * } + * </pre> */ String nextText() throws XmlPullParserException, IOException; |