Code example for XmlPullParser

Methods: getDepth, getNamespace, getNamespaceCount, getNamespacePrefix, getNamespaceUri, next, nextTag

0
        pullParser.setInput(new StringReader(XML));
        testPullParserNamespaces(pullParser);
    } 
 
    private void testPullParserNamespaces(XmlPullParser parser) throws Exception {
        assertEquals(0, parser.getDepth());
        assertEquals(0, parser.getNamespaceCount(0));
 
        try { 
            parser.getNamespaceCount(1);
            fail();
        } catch (IndexOutOfBoundsException e) { /* expected */ }
 
        // one 
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals(1, parser.getDepth());
 
        checkNamespacesInOne(parser);
 
        // n1:two 
        assertEquals(XmlPullParser.START_TAG, parser.nextTag());
 
        assertEquals(2, parser.getDepth());
        checkNamespacesInTwo(parser);
 
        // Body of two. 
        assertEquals(XmlPullParser.TEXT, parser.next());
 
        // End of two. 
        assertEquals(XmlPullParser.END_TAG, parser.nextTag());
 
        // Depth should still be 2. 
        assertEquals(2, parser.getDepth());
 
        // We should still be able to see the namespaces from two. 
        checkNamespacesInTwo(parser);
 
        // End of one. 
        assertEquals(XmlPullParser.END_TAG, parser.nextTag());
 
        // Depth should be back to 1. 
        assertEquals(1, parser.getDepth());
 
        // We can still see the namespaces in one. 
        checkNamespacesInOne(parser);
 
        // We shouldn't be able to see the namespaces in two anymore. 
        try { 
            parser.getNamespaceCount(2);
            fail();
        } catch (IndexOutOfBoundsException e) { /* expected */ }
 
        assertEquals(XmlPullParser.END_DOCUMENT, parser.next());
 
        // We shouldn't be able to see the namespaces in one anymore. 
        try { 
            parser.getNamespaceCount(1);
            fail();
        } catch (IndexOutOfBoundsException e) { /* expected */ }
 
        assertEquals(0, parser.getNamespaceCount(0));
    } 
 
    private void checkNamespacesInOne(XmlPullParser parser) throws XmlPullParserException {
        assertEquals(2, parser.getNamespaceCount(1));
 
        // Prefix for default namespace is null. 
        assertNull(parser.getNamespacePrefix(0));
        assertEquals("ns:default", parser.getNamespaceUri(0));
 
        assertEquals("n1", parser.getNamespacePrefix(1));
        assertEquals("ns:1", parser.getNamespaceUri(1));
 
        assertEquals("ns:default", parser.getNamespace(null));
 
        // KXML returns null. 
        // assertEquals("ns:default", parser.getNamespace("")); 
    } 
 
    private void checkNamespacesInTwo(XmlPullParser parser) throws XmlPullParserException {
        // These should still be valid. 
        checkNamespacesInOne(parser);
 
        assertEquals(3, parser.getNamespaceCount(2));
 
        // Default ns should still be in the stack 
        assertNull(parser.getNamespacePrefix(0));
        assertEquals("ns:default", parser.getNamespaceUri(0));
    } 
 
    public void testNamespaces() { 
        try { 
            NamespaceHandler handler = new NamespaceHandler();