Search in sources :

Example 31 with DocumentFragment

use of org.w3c.dom.DocumentFragment in project webservices-axiom by apache.

the class TestReplaceChildFirstWithDocumentFragment method runTest.

protected void runTest() throws Throwable {
    Document document = dbf.newDocumentBuilder().newDocument();
    DocumentFragment fragment = document.createDocumentFragment();
    Element x = document.createElementNS(null, "x");
    Element y = document.createElementNS(null, "y");
    fragment.appendChild(x);
    fragment.appendChild(y);
    Element element = document.createElementNS(null, "parent");
    Element a = document.createElementNS(null, "a");
    Element b = document.createElementNS(null, "b");
    element.appendChild(a);
    element.appendChild(b);
    element.replaceChild(fragment, a);
    NodeList children = element.getChildNodes();
    assertEquals(3, children.getLength());
    assertSame(x, children.item(0));
    assertSame(y, children.item(1));
    assertSame(b, children.item(2));
    assertSame(element, x.getParentNode());
    assertSame(element, y.getParentNode());
    assertNull(fragment.getFirstChild());
    assertNull(fragment.getLastChild());
    assertEquals(0, fragment.getChildNodes().getLength());
    assertSame(x, element.getFirstChild());
    assertSame(b, element.getLastChild());
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) DocumentFragment(org.w3c.dom.DocumentFragment)

Example 32 with DocumentFragment

use of org.w3c.dom.DocumentFragment in project webservices-axiom by apache.

the class TestReplaceChildMiddleWithDocumentFragment method runTest.

protected void runTest() throws Throwable {
    Document document = dbf.newDocumentBuilder().newDocument();
    DocumentFragment fragment = document.createDocumentFragment();
    Element x = document.createElementNS(null, "x");
    Element y = document.createElementNS(null, "y");
    fragment.appendChild(x);
    fragment.appendChild(y);
    Element element = document.createElementNS(null, "parent");
    Element a = document.createElementNS(null, "a");
    Element b = document.createElementNS(null, "b");
    Element c = document.createElementNS(null, "c");
    element.appendChild(a);
    element.appendChild(b);
    element.appendChild(c);
    element.replaceChild(fragment, b);
    NodeList children = element.getChildNodes();
    assertEquals(4, children.getLength());
    assertSame(a, children.item(0));
    assertSame(x, children.item(1));
    assertSame(y, children.item(2));
    assertSame(c, children.item(3));
    assertSame(element, x.getParentNode());
    assertSame(element, y.getParentNode());
    assertNull(fragment.getFirstChild());
    assertNull(fragment.getLastChild());
    assertEquals(0, fragment.getChildNodes().getLength());
    assertSame(a, element.getFirstChild());
    assertSame(c, element.getLastChild());
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) DocumentFragment(org.w3c.dom.DocumentFragment)

Aggregations

DocumentFragment (org.w3c.dom.DocumentFragment)32 Document (org.w3c.dom.Document)20 NodeList (org.w3c.dom.NodeList)17 Node (org.w3c.dom.Node)13 Element (org.w3c.dom.Element)10 Text (org.w3c.dom.Text)4 IOException (java.io.IOException)3 LinkedHashMap (java.util.LinkedHashMap)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 DOMException (org.w3c.dom.DOMException)3 HashMap (java.util.HashMap)2 DTM (org.apache.xml.dtm.DTM)2 NodeSetDTM (org.apache.xpath.NodeSetDTM)2 InputSource (org.xml.sax.InputSource)2 SAXException (org.xml.sax.SAXException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 MessagingException (javax.mail.MessagingException)1