Search in sources :

Example 41 with DOMResult

use of javax.xml.transform.dom.DOMResult in project spring-framework by spring-projects.

the class AbstractMarshallerTests method marshalEmptyDOMResult.

@Test
public void marshalEmptyDOMResult() throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
    DOMResult domResult = new DOMResult();
    marshaller.marshal(flights, domResult);
    assertTrue("DOMResult does not contain a Document", domResult.getNode() instanceof Document);
    Document result = (Document) domResult.getNode();
    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 42 with DOMResult

use of javax.xml.transform.dom.DOMResult in project spring-framework by spring-projects.

the class AbstractStaxHandlerTestCase method noNamespacePrefixesDom.

@Test
public void noNamespacePrefixesDom() throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document expected = documentBuilder.parse(new InputSource(new StringReader(SIMPLE_XML)));
    Document result = documentBuilder.newDocument();
    AbstractStaxHandler handler = createStaxHandler(new DOMResult(result));
    xmlReader.setContentHandler(handler);
    xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
    xmlReader.setFeature("http://xml.org/sax/features/namespaces", true);
    xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
    xmlReader.parse(new InputSource(new StringReader(SIMPLE_XML)));
    assertThat(result, isSimilarTo(expected).withNodeFilter(nodeFilter));
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DOMResult(javax.xml.transform.dom.DOMResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 43 with DOMResult

use of javax.xml.transform.dom.DOMResult in project spring-framework by spring-projects.

the class AbstractStaxHandlerTestCase method namespacePrefixesDom.

@Test
public void namespacePrefixesDom() throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document expected = documentBuilder.parse(new InputSource(new StringReader(SIMPLE_XML)));
    Document result = documentBuilder.newDocument();
    AbstractStaxHandler handler = createStaxHandler(new DOMResult(result));
    xmlReader.setContentHandler(handler);
    xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
    xmlReader.setFeature("http://xml.org/sax/features/namespaces", true);
    xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
    xmlReader.parse(new InputSource(new StringReader(SIMPLE_XML)));
    assertThat(expected, isSimilarTo(result).withNodeFilter(nodeFilter));
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DOMResult(javax.xml.transform.dom.DOMResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 44 with DOMResult

use of javax.xml.transform.dom.DOMResult in project spring-framework by spring-projects.

the class AbstractStaxXMLReaderTestCase method whitespace.

@Test
public void whitespace() throws Exception {
    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test><node1> </node1><node2> Some text </node2></test>";
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    AbstractStaxXMLReader staxXmlReader = createStaxXmlReader(new ByteArrayInputStream(xml.getBytes("UTF-8")));
    SAXSource source = new SAXSource(staxXmlReader, new InputSource());
    DOMResult result = new DOMResult();
    transformer.transform(source, result);
    Node node1 = result.getNode().getFirstChild().getFirstChild();
    assertEquals(" ", node1.getTextContent());
    assertEquals(" Some text ", node1.getNextSibling().getTextContent());
}
Also used : InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) SAXSource(javax.xml.transform.sax.SAXSource) DOMResult(javax.xml.transform.dom.DOMResult) ByteArrayInputStream(java.io.ByteArrayInputStream) Node(org.w3c.dom.Node) Test(org.junit.Test)

Example 45 with DOMResult

use of javax.xml.transform.dom.DOMResult in project spring-framework by spring-projects.

the class StaxSourceTests method eventReaderSourceToDOMResult.

@Test
public void eventReaderSourceToDOMResult() throws Exception {
    XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(XML));
    StaxSource source = new StaxSource(eventReader);
    assertEquals("Invalid eventReader returned", eventReader, source.getXMLEventReader());
    assertNull("StreamReader returned", source.getXMLStreamReader());
    Document expected = documentBuilder.parse(new InputSource(new StringReader(XML)));
    Document result = documentBuilder.newDocument();
    transformer.transform(source, new DOMResult(result));
    assertThat("Invalid result", result, isSimilarTo(expected));
}
Also used : InputSource(org.xml.sax.InputSource) DOMResult(javax.xml.transform.dom.DOMResult) StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) Document(org.w3c.dom.Document) Test(org.junit.Test)

Aggregations

DOMResult (javax.xml.transform.dom.DOMResult)61 Document (org.w3c.dom.Document)33 DOMSource (javax.xml.transform.dom.DOMSource)24 Transformer (javax.xml.transform.Transformer)20 DocumentBuilder (javax.xml.parsers.DocumentBuilder)17 TransformerException (javax.xml.transform.TransformerException)14 IOException (java.io.IOException)13 InputSource (org.xml.sax.InputSource)13 StreamSource (javax.xml.transform.stream.StreamSource)12 StringReader (java.io.StringReader)11 SAXResult (javax.xml.transform.sax.SAXResult)10 Element (org.w3c.dom.Element)10 Node (org.w3c.dom.Node)10 SAXSource (javax.xml.transform.sax.SAXSource)9 StreamResult (javax.xml.transform.stream.StreamResult)9 Test (org.junit.Test)9 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)8 Source (javax.xml.transform.Source)8 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)7 InputStream (java.io.InputStream)6