Code example for XmlPullParser

Methods: getName, getNamespace, next, setFeature, setInput

0
        assertEquals("mac&cheese", parser.getText());
    } 
 
    public void testEntitiesAndNamespaces() throws Exception { 
        XmlPullParser parser = newPullParser();
        parser.setFeature("http://xmlpull.org/v1/doc/features.html#process-namespaces", true);
        parser.setInput(new StringReader(
                "<foo:a xmlns:foo='http://foo' xmlns:bar='http://bar'><bar:b/></foo:a>")); 
        testNamespace(parser);
    } 
 
    public void testEntitiesAndNamespacesWithRelaxed() throws Exception { 
        XmlPullParser parser = newPullParser();
        parser.setFeature("http://xmlpull.org/v1/doc/features.html#process-namespaces", true);
        parser.setFeature("http://xmlpull.org/v1/doc/features.html#relaxed", true);
        parser.setInput(new StringReader(
                "<foo:a xmlns:foo='http://foo' xmlns:bar='http://bar'><bar:b/></foo:a>")); 
        testNamespace(parser);
    } 
 
    private void testNamespace(XmlPullParser parser) throws Exception {
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals("http://foo", parser.getNamespace());
        assertEquals("a", parser.getName());
        assertEquals(XmlPullParser.START_TAG, parser.next());
        assertEquals("http://bar", parser.getNamespace());
        assertEquals("b", parser.getName());
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals("http://bar", parser.getNamespace());
        assertEquals("b", parser.getName());
        assertEquals(XmlPullParser.END_TAG, parser.next());
        assertEquals("http://foo", parser.getNamespace());
        assertEquals("a", parser.getName());
    } 
 
    public void testRegularNumericEntities() throws Exception { 
        XmlPullParser parser = newPullParser();
        parser.setInput(new StringReader("<foo>&#65;</foo>"));