Code example for Document

Methods: getElementsByTagName

0
        XmlContext context = new XmlContext(driver, project, null, file, null);
        Document document = parser.parseXml(context);
        assertNotNull(document);
 
        // Basic parsing heart beat tests 
        Element linearLayout = (Element) document.getElementsByTagName("LinearLayout").item(0);
        assertNotNull(linearLayout);
        NodeList buttons = document.getElementsByTagName("Button");
        assertEquals(2, buttons.getLength());
        final String ANDROID_URI = "http://schemas.android.com/apk/res/android";
        assertEquals("wrap_content",
                linearLayout.getAttributeNS(ANDROID_URI, "layout_height"));
 
        // Check attribute positions 
        Attr attr = linearLayout.getAttributeNodeNS(ANDROID_URI, "layout_width");
        assertNotNull(attr);
        Location location = parser.getLocation(context, attr);
        Position start = location.getStart();
        Position end = location.getEnd();
        assertEquals(2, start.getLine());
        assertEquals(xml.indexOf("android:layout_width"), start.getOffset());
        assertEquals(2, end.getLine());
        String target = "android:layout_width=\"match_parent\"";
        assertEquals(xml.indexOf(target) + target.length(), end.getOffset());
 
        // Check element positions 
        Element button = (Element) buttons.item(0);
        location = parser.getLocation(context, button);
        start = location.getStart();
        end = location.getEnd();
        assertEquals(6, start.getLine());
        assertEquals(xml.indexOf("<Button"), start.getOffset());
        assertEquals(xml.indexOf("/>") + 2, end.getOffset());
        assertEquals(10, end.getLine());
        int button1End = end.getOffset();
 
        Handle handle = parser.createLocationHandle(context, button);
        Location location2 = handle.resolve();
        assertSame(location.getFile(), location.getFile());
        assertNotNull(location2.getStart());
        assertNotNull(location2.getEnd());
        assertEquals(6, location2.getStart().getLine());
        assertEquals(10, location2.getEnd().getLine());
 
        Element button2 = (Element) buttons.item(1);
        location = parser.getLocation(context, button2);
        start = location.getStart();
        end = location.getEnd();
        assertEquals(12, start.getLine());
        assertEquals(xml.indexOf("<Button", button1End), start.getOffset());
        assertEquals(xml.indexOf("/>", start.getOffset()) + 2, end.getOffset());
        assertEquals(16, end.getLine());
 
        parser.dispose(context, document);
 
        file.delete();
    } 
 
    public void testLineEndings() throws Exception {