Code example for XmlPullParser

Methods: getName, getText, next, setInput

0
                + "  <!ELEMENT foo (bar)*>\n" 
                + "  <!ELEMENT bar ANY>\n" 
                + "]>" 
                + "<foo>  \n  <bar></bar>  \t  </foo>"; 
        XmlPullParser parser = newPullParser(xml);
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals("foo", parser.getName());
        assertEquals(XmlPullParser.TEXT, parser.next());
        assertEquals("  \n  ", parser.getText());
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals("bar", parser.getName());
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals("bar", parser.getName());
        assertEquals(XmlPullParser.TEXT, parser.next());
        assertEquals("  \t  ", parser.getText());
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals(XmlPullParser.END_DOCUMENT, parser.next());
    } 
 
    public void testEmptyDoesNotInformIgnorableWhitespace() throws Exception { 
        String xml = "<!DOCTYPE foo [\n"
                + "  <!ELEMENT foo EMPTY>\n" 
                + "]>" 
                + "<foo>  \n  </foo>"; 
        XmlPullParser parser = newPullParser(xml);
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals("foo", parser.getName());
        assertEquals(XmlPullParser.TEXT, parser.next());
        assertEquals("  \n  ", parser.getText());
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals(XmlPullParser.END_DOCUMENT, parser.next());
    } 
 
    /** 
     * Test that the parser doesn't expand the entity attributes. 
     */ 
    public void testAttributeOfTypeEntity() throws Exception { 
        String xml = "<!DOCTYPE foo [\n"
                + "  <!ENTITY a \"android\">" 
                + "  <!ELEMENT foo ANY>\n" 
                + "  <!ATTLIST foo\n" 
                + "    bar ENTITY #IMPLIED>" 
                + "]>" 
                + "<foo bar=\"a\"></foo>"; 
        XmlPullParser parser = newPullParser(xml);
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals("foo", parser.getName());
        assertEquals("a", parser.getAttributeValue(null, "bar"));
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals(XmlPullParser.END_DOCUMENT, parser.next());
    } 
 
    public void testTagStructureNotValidated() throws Exception { 
        String xml = "<!DOCTYPE foo [\n"
                + "  <!ELEMENT foo (bar)*>\n" 
                + "  <!ELEMENT bar ANY>\n" 
                + "  <!ELEMENT baz ANY>\n" 
                + "]>" 
                + "<foo><bar/><bar/><baz/></foo>"; 
        XmlPullParser parser = newPullParser(xml);
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals("foo", parser.getName());
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals("bar", parser.getName());
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals("bar", parser.getName());
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals("baz", parser.getName());
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals(XmlPullParser.END_DOCUMENT, parser.next());
    } 
 
    public void testAttributeDefaultValues() throws Exception { 
        String xml = "<!DOCTYPE foo [\n"
                + "  <!ATTLIST bar\n" 
                + "    baz (a|b|c)  \"c\">" 
                + "]>" 
                + "<foo>" 
                + "<bar/>" 
                + "<bar baz=\"a\"/>" 
                + "</foo>"; 
        XmlPullParser parser = newPullParser(xml);
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals("bar", parser.getName());
        assertEquals("c", parser.getAttributeValue(null, "baz"));
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals("bar", parser.getName());
        assertEquals("a", parser.getAttributeValue(null, "baz"));
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals(XmlPullParser.END_DOCUMENT, parser.next());
    } 
 
    public void testAttributeDefaultValueEntitiesExpanded() throws Exception { 
        String xml = "<!DOCTYPE foo [\n"
                + "  <!ENTITY g \"ghi\">" 
                + "  <!ELEMENT foo ANY>\n" 
                + "  <!ATTLIST foo\n" 
                + "    bar CDATA \"abc &amp; def &g; jk\">" 
                + "]>" 
                + "<foo></foo>"; 
        XmlPullParser parser = newPullParser(xml);
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals("foo", parser.getName());
        assertEquals("abc & def ghi jk", parser.getAttributeValue(null, "bar"));
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals(XmlPullParser.END_DOCUMENT, parser.next());
    } 
 
    public void testAttributeDefaultValuesAndNamespaces() throws Exception { 
        String xml = "<!DOCTYPE foo [\n"
                + "  <!ATTLIST foo\n" 
                + "    bar:a CDATA \"android\">" 
                + "]>" 
                + "<foo xmlns:bar='http://bar'></foo>"; 
        XmlPullParser parser = newPullParser(xml);
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals("foo", parser.getName());
        // In Expat, namespaces don't apply to default attributes 
        int index = indexOfAttributeWithName(parser, "bar:a");
        assertEquals("", parser.getAttributeNamespace(index));
        assertEquals("bar:a", parser.getAttributeName(index));
        assertEquals("android", parser.getAttributeValue(index));
        assertEquals("CDATA", parser.getAttributeType(index));
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals(XmlPullParser.END_DOCUMENT, parser.next());
    } 
 
    private int indexOfAttributeWithName(XmlPullParser parser, String name) {
        for (int i = 0; i < parser.getAttributeCount(); i++) {
            if (parser.getAttributeName(i).equals(name)) {
                return i;
            } 
        } 
        return -1; 
    } 
 
    public void testAttributeEntitiesExpandedEagerly() throws Exception { 
        assertParseFailure("<!DOCTYPE foo [\n" 
                + "  <!ELEMENT foo ANY>\n" 
                + "  <!ATTLIST foo\n" 
                + "    bar CDATA \"abc &amp; def &g; jk\">" 
                + "  <!ENTITY g \"ghi\">" 
                + "]>" 
                + "<foo></foo>"); 
    } 
 
    public void testRequiredAttributesOmitted() throws Exception { 
        String xml = "<!DOCTYPE foo [\n"
                + "  <!ELEMENT foo ANY>\n" 
                + "  <!ATTLIST foo\n" 
                + "    bar (a|b|c) #REQUIRED>" 
                + "]>" 
                + "<foo></foo>"; 
        XmlPullParser parser = newPullParser(xml);
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals("foo", parser.getName());
        assertEquals(null, parser.getAttributeValue(null, "bar"));
        assertEquals(XmlPullParser.END_TAG, parser.next());
    } 
 
    public void testFixedAttributesWithConflictingValues() throws Exception { 
        String xml = "<!DOCTYPE foo [\n"
                + "  <!ELEMENT foo ANY>\n" 
                + "  <!ATTLIST foo\n" 
                + "    bar (a|b|c) #FIXED \"c\">" 
                + "]>" 
                + "<foo bar=\"a\"></foo>"; 
        XmlPullParser parser = newPullParser(xml);
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals("foo", parser.getName());
        assertEquals("a", parser.getAttributeValue(null, "bar"));
        assertEquals(XmlPullParser.END_TAG, parser.next());
    } 
 
    public void testParsingNotations() throws Exception { 
        String xml = "<!DOCTYPE foo [\n"
                + "  <!NOTATION type-a PUBLIC \"application/a\"> \n" 
                + "  <!NOTATION type-b PUBLIC \"image/b\">\n" 
                + "  <!NOTATION type-c PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n" 
                + "     \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"> \n" 
                + "  <!ENTITY file          SYSTEM \"d.xml\">\n" 
                + "  <!ENTITY fileWithNdata SYSTEM \"e.bin\" NDATA type-b>\n" 
                + "]>" 
                + "<foo type=\"type-a\"/>"; 
        XmlPullParser parser = newPullParser(xml);
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals("foo", parser.getName());
        assertEquals(XmlPullParser.END_TAG, parser.next());
    } 
 
    public void testCommentsInDoctype() throws Exception { 
        String xml = "<!DOCTYPE foo ["
                + "  <!-- ' -->" 
                + "]><foo>android</foo>"; 
        XmlPullParser parser = newPullParser(xml);
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals(XmlPullParser.TEXT, parser.next());
        assertEquals("android", parser.getText());
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals(XmlPullParser.END_DOCUMENT, parser.next());
    } 
 
    public void testDoctypeNameOnly() throws Exception { 
        String xml = "<!DOCTYPE foo><foo></foo>";
        XmlPullParser parser = newPullParser(xml);
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals("foo", parser.getName());
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals("foo", parser.getName());
        assertEquals(XmlPullParser.END_DOCUMENT, parser.next());
    } 
 
    public void testVeryLongEntities() throws Exception { 
        String a = repeat('a', READ_BUFFER_SIZE + 1);
        String b = repeat('b', READ_BUFFER_SIZE + 1);
        String c = repeat('c', READ_BUFFER_SIZE + 1);
 
        String xml = "<!DOCTYPE foo [\n"
                + "  <!ENTITY " + a + "  \"d &" + b + "; e\">"
                + "  <!ENTITY " + b + "  \"f " + c + " g\">"
                + "]>" 
                + "<foo>h &" + a + "; i</foo>";
        XmlPullParser parser = newPullParser(xml);
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals(XmlPullParser.TEXT, parser.next());
        assertEquals("h d f " + c + " g e i", parser.getText());
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals(XmlPullParser.END_DOCUMENT, parser.next());
    } 
 
    public void testManuallyRegisteredEntitiesWithDoctypeParsing() throws Exception { 
        String xml = "<foo>&a;</foo>";
        XmlPullParser parser = newPullParser(xml);
        try { 
            parser.defineEntityReplacementText("a", "android");
            fail();
        } catch (UnsupportedOperationException expected) {
        } catch (IllegalStateException expected) {
        } 
    } 
 
    public void testDoctypeWithNextToken() throws Exception { 
        String xml = "<!DOCTYPE foo [<!ENTITY bb \"bar baz\">]><foo>a&bb;c</foo>";
        XmlPullParser parser = newPullParser(xml);
        assertEquals(XmlPullParser.DOCDECL, parser.nextToken());
        assertEquals(" foo [<!ENTITY bb \"bar baz\">]", parser.getText());
        assertNull(parser.getName());
        assertEquals(XmlPullParser.START_TAG, parser.nextToken());
        assertEquals(XmlPullParser.TEXT, parser.next());
        assertEquals("abar bazc", parser.getText());
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals(XmlPullParser.END_DOCUMENT, parser.next());
    } 
 
    public void testDoctypeSpansBuffers() throws Exception { 
        char[] doctypeChars = new char[READ_BUFFER_SIZE + 1];
        Arrays.fill(doctypeChars, 'x');
        String doctypeBody = " foo [<!--" + new String(doctypeChars) + "-->]";
        String xml = "<!DOCTYPE" + doctypeBody + "><foo/>";
        XmlPullParser parser = newPullParser(xml);
        assertEquals(XmlPullParser.DOCDECL, parser.nextToken());
        assertEquals(doctypeBody, parser.getText());
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals(XmlPullParser.END_DOCUMENT, parser.next());
    } 
 
    public void testDoctypeInDocumentElement() throws Exception { 
        assertParseFailure("<foo><!DOCTYPE foo></foo>"); 
    } 
 
    public void testDoctypeAfterDocumentElement() throws Exception { 
        assertParseFailure("<foo/><!DOCTYPE foo>"); 
    } 
 
    private void assertParseFailure(String xml) throws Exception {
        XmlPullParser parser = newPullParser();
        parser.setInput(new StringReader(xml));
        try { 
            while (parser.next() != XmlPullParser.END_DOCUMENT) {
            } 
            fail();
        } catch (XmlPullParserException expected) {
        } 
    } 
 
    private String repeat(char c, int length) {
        char[] chars = new char[length];
        Arrays.fill(chars, c);
        return new String(chars);
    } 
 
    private XmlPullParser newPullParser(String xml) throws XmlPullParserException {
        XmlPullParser result = newPullParser();
        result.setInput(new StringReader(xml));
        return result;
    } 
 
    /** 
     * Creates a new pull parser.