Search in sources :

Example 31 with Text

use of org.w3c.dom.Text in project robovm by robovm.

the class DomTest method testCloneNode.

public void testCloneNode() throws Exception {
    document = builder.parse(new InputSource(new StringReader("<menu " + "xmlns:f=\"http://food\" xmlns:a=\"http://addons\">" + "<f:item a:standard=\"strawberry\" deluxe=\"yes\">Waffles</f:item></menu>")));
    name = (Element) document.getFirstChild().getFirstChild();
    Element clonedName = (Element) name.cloneNode(true);
    assertNull(clonedName.getParentNode());
    assertNull(clonedName.getNextSibling());
    assertNull(clonedName.getPreviousSibling());
    assertEquals("http://food", clonedName.getNamespaceURI());
    assertEquals("f:item", clonedName.getNodeName());
    assertEquals("item", clonedName.getLocalName());
    assertEquals("http://food", clonedName.getNamespaceURI());
    assertEquals("yes", clonedName.getAttribute("deluxe"));
    assertEquals("strawberry", clonedName.getAttribute("a:standard"));
    assertEquals("strawberry", clonedName.getAttributeNS("http://addons", "standard"));
    assertEquals(1, name.getChildNodes().getLength());
    Text clonedChild = (Text) clonedName.getFirstChild();
    assertSame(clonedName, clonedChild.getParentNode());
    assertNull(clonedChild.getNextSibling());
    assertNull(clonedChild.getPreviousSibling());
    assertEquals("Waffles", clonedChild.getTextContent());
}
Also used : InputSource(org.xml.sax.InputSource) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Text(org.w3c.dom.Text)

Example 32 with Text

use of org.w3c.dom.Text in project robovm by robovm.

the class DomTest method testCoalescingOn.

public void testCoalescingOn() throws Exception {
    String xml = "<foo>abc<![CDATA[def]]>ghi</foo>";
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setCoalescing(true);
    document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
    Element documentElement = document.getDocumentElement();
    Text text = (Text) documentElement.getFirstChild();
    assertEquals("abcdefghi", text.getTextContent());
    assertNull(text.getNextSibling());
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Text(org.w3c.dom.Text)

Example 33 with Text

use of org.w3c.dom.Text in project robovm by robovm.

the class DomTest method testExpandingEntityReferencesOn.

public void testExpandingEntityReferencesOn() throws Exception {
    String xml = "<!DOCTYPE foo [ <!ENTITY def \"DEF\"> ]>" + "<foo>abc&def;ghi</foo>";
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setExpandEntityReferences(true);
    document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
    Element documentElement = document.getDocumentElement();
    Text text = (Text) documentElement.getFirstChild();
    assertEquals("This implementation doesn't expand entity references", "abcDEFghi", text.getTextContent());
    assertNull(text.getNextSibling());
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Text(org.w3c.dom.Text)

Example 34 with Text

use of org.w3c.dom.Text in project robovm by robovm.

the class DomTest method testCoalescingOff.

public void testCoalescingOff() throws Exception {
    String xml = "<foo>abc<![CDATA[def]]>ghi</foo>";
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setCoalescing(false);
    document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
    Element documentElement = document.getDocumentElement();
    Text abc = (Text) documentElement.getFirstChild();
    assertEquals("abc", abc.getTextContent());
    CDATASection def = (CDATASection) abc.getNextSibling();
    assertEquals("def", def.getTextContent());
    Text ghi = (Text) def.getNextSibling();
    assertEquals("ghi", ghi.getTextContent());
    assertNull(ghi.getNextSibling());
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) CDATASection(org.w3c.dom.CDATASection) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Text(org.w3c.dom.Text)

Example 35 with Text

use of org.w3c.dom.Text in project robovm by robovm.

the class DomTest method testReplaceWholeTextOnly.

public void testReplaceWholeTextOnly() throws TransformerException {
    String original = domToString(document);
    Text replacement = vitamincText.replaceWholeText("70%");
    assertEquals(Node.TEXT_NODE, replacement.getNodeType());
    assertSame(vitamincText, replacement);
    String expected = original.replace("60%", "70%");
    assertEquals(expected, domToString(document));
}
Also used : Text(org.w3c.dom.Text)

Aggregations

Text (org.w3c.dom.Text)166 Element (org.w3c.dom.Element)93 Document (org.w3c.dom.Document)64 Node (org.w3c.dom.Node)58 NodeList (org.w3c.dom.NodeList)35 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)23 DocumentBuilder (javax.xml.parsers.DocumentBuilder)22 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)17 IOException (java.io.IOException)14 Attr (org.w3c.dom.Attr)14 InputSource (org.xml.sax.InputSource)11 StringReader (java.io.StringReader)10 SAXException (org.xml.sax.SAXException)8 ArrayList (java.util.ArrayList)7 DOMException (org.w3c.dom.DOMException)7 Test (org.junit.Test)6 DOMSource (javax.xml.transform.dom.DOMSource)5 NamedNodeMap (org.w3c.dom.NamedNodeMap)5 HashMap (java.util.HashMap)4 DocumentFragment (org.w3c.dom.DocumentFragment)4