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));
}
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));
}
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));
}
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());
}
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));
}
Aggregations