Search in sources :

Example 16 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 17 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 18 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 19 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 20 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)144 Element (org.w3c.dom.Element)82 Document (org.w3c.dom.Document)51 Node (org.w3c.dom.Node)48 NodeList (org.w3c.dom.NodeList)27 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)20 DocumentBuilder (javax.xml.parsers.DocumentBuilder)19 Attr (org.w3c.dom.Attr)12 StringReader (java.io.StringReader)10 InputSource (org.xml.sax.InputSource)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)9 DOMException (org.w3c.dom.DOMException)7 IOException (java.io.IOException)6 Test (org.junit.Test)6 HashMap (java.util.HashMap)4 DOMSource (javax.xml.transform.dom.DOMSource)4 DocumentFragment (org.w3c.dom.DocumentFragment)4 NamedNodeMap (org.w3c.dom.NamedNodeMap)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3