Re: Re: Questions.

Aleksander Slominski <[email protected]>
Newsgroups gmane.text.xml.xmlpull.devel
Message-ID <[email protected]>
lrargerich wrote:

> --- In xmlpull-dev@y..., Stefan Haustein <haustein@k...> wrote:
> > > 3) Does the nextText method "eats" the current event passing the
> parser
> > >    to the text event (or end_tag) or it just returns the next text
> > >    content but the parser remains in its actual state?
> >
> > nextText "eats" the text; otherwise, the state would be inconsistent
> > for <tag></tag> vs. <tag>text</tag>
> >
> > Best,
> > Stefan
>
> So if you do a getType after nextText does it return TEXT or whatever
> is "next" to the text ? (ie: END_TAG or START_TAG, never TEXT because
> there won't be two TEXT events together)

hi,

nextText() function "eats" text and parser is positioned alwasy on END_TAG.

here is small test case to verify it:

    public void testNextText() throws Exception {
        final String INPUT_XML =
            "<t><test1>foo</test1><test2></test2><test3/><test4>bar</test4></t>";
        XmlPullParser pp = factory.newPullParser();
        pp.setInput( new StringReader( INPUT_XML ) );
        pp.next();
        pp.require( pp.START_TAG, null, "t");
        pp.next();
        pp.require( pp.START_TAG, null, "test1");
        assertEquals( "foo", pp.nextText() );
        pp.require( pp.END_TAG, null, "test1");

        pp.next();
        pp.require( pp.START_TAG, null, "test2");
        assertEquals( "", pp.nextText() );
        pp.require( pp.END_TAG, null, "test2");

        pp.next();
        pp.require( pp.START_TAG, null, "test3");
        assertEquals( "", pp.nextText() );
        pp.require( pp.END_TAG, null, "test3");

        pp.next();
        pp.require( pp.START_TAG, null, "test4");
        //pp.next();
        //pp.require( pp.TEXT, null, null);
        assertEquals( "bar", pp.nextText() );
        pp.require( pp.END_TAG, null, "test4");

        pp.next();
        pp.require( pp.END_TAG, null, "t");
        pp.next();
        pp.require( pp.END_DOCUMENT, null, null);

        // now check for error conditions
        pp.setInput( new StringReader( INPUT_XML ) );
        pp.next();
        pp.require( pp.START_TAG, null, "t");
        pp.next();
        pp.require( pp.START_TAG, null, "test1");
        pp.next();
        pp.require( pp.TEXT, null, null);
        try {
            pp.nextText();
            fail("if current tag is TEXT no next text content can be returned!");
        } catch(XmlPullParserException ex) {}

        pp.setInput( new StringReader( INPUT_XML ) );
        pp.next();
        pp.require( pp.START_TAG, null, "t");
        try {
            pp.nextText();
            fail("if next tag is START_TAG no text content can be returned!");
        } catch(XmlPullParserException ex) {}

        pp.setInput( new StringReader( INPUT_XML ) );
        pp.next();
        pp.require( pp.START_TAG, null, "t");
        pp.next();
        pp.require( pp.START_TAG, null, "test1");
        pp.next();
        pp.next();
        pp.require( pp.END_TAG, null, "test1");
        try {
            pp.nextText();
            fail("if current tag is END_TAG no text content can be returned!");
        } catch(XmlPullParserException ex) {}

    }

thanks,

alek
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.