Search in sources :

Example 21 with Text

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

the class DomTest method testReplaceWholeTextLast.

public void testReplaceWholeTextLast() throws TransformerException {
    String original = domToString(document);
    Text replacement = descriptionText3.replaceWholeText("Eggos");
    assertSame(descriptionText3, replacement);
    String expected = original.replace("Belgian<![CDATA[ waffles & strawberries (< 5g ]]>of fat)", "Eggos");
    assertEquals("This implementation doesn't remove preceding nodes in replaceWholeText()", expected, domToString(document));
}
Also used : Text(org.w3c.dom.Text)

Example 22 with Text

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

the class DomTest method testIsElementContentWhitespaceWithDeclaration.

public void testIsElementContentWhitespaceWithDeclaration() throws Exception {
    String xml = "<!DOCTYPE menu [\n" + "  <!ELEMENT menu (item)*>\n" + "  <!ELEMENT item (#PCDATA)>\n" + "]><menu>    <item/>   </menu>";
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Text text = (Text) factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml))).getDocumentElement().getChildNodes().item(0);
    assertTrue("This implementation does not recognize element content whitespace", text.isElementContentWhitespace());
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StringReader(java.io.StringReader) Text(org.w3c.dom.Text)

Example 23 with Text

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

the class DomTest method testExpandingEntityReferencesOff.

public void testExpandingEntityReferencesOff() throws Exception {
    String xml = "<!DOCTYPE foo [ <!ENTITY def \"DEF\"> ]>" + "<foo>abc&def;ghi</foo>";
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setExpandEntityReferences(false);
    document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
    Element documentElement = document.getDocumentElement();
    Text abc = (Text) documentElement.getFirstChild();
    assertEquals("abc", abc.getTextContent());
    EntityReference def = (EntityReference) abc.getNextSibling();
    assertEquals("def", def.getNodeName());
    Text ghi = (Text) def.getNextSibling();
    assertNull(ghi.getNextSibling());
    /*
         * We expect the entity reference to contain one child Text node "DEF".
         * The RI's entity reference contains no children. Instead it stashes
         * "DEF" in the next sibling node.
         */
    assertEquals("Expected text value only and no expanded entity data", "ghi", ghi.getTextContent());
    NodeList defChildren = def.getChildNodes();
    assertEquals("This implementation doesn't include children in entity references", 1, defChildren.getLength());
    assertEquals("DEF", defChildren.item(0).getTextContent());
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) StringReader(java.io.StringReader) EntityReference(org.w3c.dom.EntityReference) Text(org.w3c.dom.Text)

Example 24 with Text

use of org.w3c.dom.Text in project spring-framework by spring-projects.

the class AbstractMarshallerTests method marshalDOMResult.

@Test
public void marshalDOMResult() throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
    Document result = builder.newDocument();
    DOMResult domResult = new DOMResult(result);
    marshaller.marshal(flights, domResult);
    Document expected = builder.newDocument();
    Element flightsElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flights");
    Attr namespace = expected.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:tns");
    namespace.setNodeValue("http://samples.springframework.org/flight");
    flightsElement.setAttributeNode(namespace);
    expected.appendChild(flightsElement);
    Element flightElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flight");
    flightsElement.appendChild(flightElement);
    Element numberElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:number");
    flightElement.appendChild(numberElement);
    Text text = expected.createTextNode("42");
    numberElement.appendChild(text);
    assertThat("Marshaller writes invalid DOMResult", result, isSimilarTo(expected));
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DOMResult(javax.xml.transform.dom.DOMResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr) Test(org.junit.Test)

Example 25 with Text

use of org.w3c.dom.Text in project spring-framework by spring-projects.

the class AbstractUnmarshallerTests method unmarshalDomSource.

@Test
public void unmarshalDomSource() throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = builder.newDocument();
    Element flightsElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flights");
    document.appendChild(flightsElement);
    Element flightElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flight");
    flightsElement.appendChild(flightElement);
    Element numberElement = document.createElementNS("http://samples.springframework.org/flight", "tns:number");
    flightElement.appendChild(numberElement);
    Text text = document.createTextNode("42");
    numberElement.appendChild(text);
    DOMSource source = new DOMSource(document);
    Object flights = unmarshaller.unmarshal(source);
    testFlights(flights);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) Document(org.w3c.dom.Document) Test(org.junit.Test)

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