Refine search
@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()); }
@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)); }
TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); DOMSource domSource = new DOMSource(_domResult.getNode()); StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); transformer.transform(domSource, streamResult); _data = stringWriter.toString(); } catch (TransformerException te) {
public Object unmarshal(Source source) throws IOException, XmlMappingException { //this.writeXml(((DOMSource)source).getNode().getOwnerDocument()); DOMResult result = null; try { Transformer transformer = transformerFactory.newTransformer(); result = new DOMResult(); transformer.transform(source, result); } catch (Exception e) { throw new MarshallingFailureException("Failed to unmarshal SOAP Response", e); } Weather weather = new Weather(); String expression = xpathPrefix + "p:City"; String city = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode()); weather.setCity(city); expression = xpathPrefix + "p:State"; String state = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode()); weather.setState(state); expression = xpathPrefix + "p:Temperature"; String temperature = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode()); weather.setTemperature(temperature); expression = xpathPrefix + "p:Description"; String description = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode()); weather.setDescription(description); return weather; }
log.error("Now trying DOMResult.."); DOMResult result = new DOMResult(); org.docx4j.XmlUtils.transform(doc, xslt, transformParameters, result); org.w3c.dom.Document docResult = ((org.w3c.dom.Document)result.getNode()); Object o = XmlUtils.unmarshal(((org.w3c.dom.Document)result.getNode()) ); part.setJaxbElement(o); } else Object o = XmlUtils.unmarshal(((org.w3c.dom.Document)result.getNode()) ); part.setJaxbElement(o);
TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); DOMSource domSource = new DOMSource(_domResult.getNode()); StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); transformer.transform(domSource, streamResult); _data = stringWriter.toString(); } catch (TransformerException te) {
/** * Encodes an object directly to a dom. * * <p>Note that this method should be used for testing or convenience since it does not stream * and loads the entire encoded result into memory. */ public Document encodeAsDOM(Object object, QName name) throws IOException, SAXException, TransformerException { ByteArrayOutputStream out = new ByteArrayOutputStream(); encode(object, name, out); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); DOMResult result = new DOMResult(); Transformer tx = TransformerFactory.newInstance().newTransformer(); tx.transform(new StreamSource(in), result); return (Document) result.getNode(); }
log.error("Now trying DOMResult.."); DOMResult result = new DOMResult(); org.docx4j.XmlUtils.transform(doc, xslt, transformParameters, result); org.w3c.dom.Document docResult = ((org.w3c.dom.Document)result.getNode()); Object o = XmlUtils.unmarshal(((org.w3c.dom.Document)result.getNode()) ); part.setJaxbElement(o); } else Object o = XmlUtils.unmarshal(((org.w3c.dom.Document)result.getNode()) ); part.setJaxbElement(o);
private Document transformDocument(Document documentPayload, Transformer transformer) throws TransformerException { Source source; if (this.alwaysUseSourceFactory) { source = this.sourceFactory.createSource(documentPayload); } else { source = new DOMSource(documentPayload); } Result result = getResultFactory().createResult(documentPayload); if (!DOMResult.class.isAssignableFrom(result.getClass())) { throw new MessagingException( "Document to Document conversion requires a DOMResult-producing ResultFactory implementation."); } DOMResult domResult = (DOMResult) result; transformer.transform(source, domResult); return (Document) domResult.getNode(); }
import java.io.*; import javax.xml.stream.*; import javax.xml.transform.*; import javax.xml.transform.stax.StAXSource; import javax.xml.transform.dom.DOMResult; import org.w3c.dom.* public class Demo { public static void main(String[] args) throws Exception { XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml")); xsr.nextTag(); // Advance to statements element TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) { DOMResult result = new DOMResult(); t.transform(new StAXSource(xsr), result); Node domNode = result.getNode(); } } }
DOMResult result = new DOMResult(); XmlUtils.transform(new DOMSource(el), mcPreprocessorXslt, null, result); org.w3c.dom.Document doc = (org.w3c.dom.Document) result.getNode(); el = doc.getDocumentElement(); } catch (Exception e) { DOMResult result = new DOMResult(); Templates mcPreprocessorXslt = JaxbValidationEventHandler .getMcPreprocessor(); XmlUtils.transform(doc, mcPreprocessorXslt, null, result); doc = (org.w3c.dom.Document) result.getNode(); jaxbElement = (E) XmlUtils.unwrap(binder.unmarshal(doc)); } catch (ClassCastException cce) {
public Object transformResult(Result result) { String returnString = null; if (result instanceof StringResult) { returnString = result.toString(); } else if (result instanceof DOMResult) { try { StringResult stringResult = new StringResult(); getNewTransformer().transform( new DOMSource(((DOMResult) result).getNode()), stringResult); returnString = stringResult.toString(); } catch (TransformerException e) { throw new MessagingException("failed to transform from DOMSource failed", e); } } if (returnString == null) { throw new MessagingException("failed to convert Result type [" + result.getClass().getName() + "] to string"); } return returnString; }
DOMResult result = new DOMResult(); XmlUtils.transform(new StreamSource(is), mcPreprocessorXslt, null, result); doc = (org.w3c.dom.Document) result.getNode(); } catch (Exception e) { throw new JAXBException("Preprocessing exception", e); DOMResult result = new DOMResult(); doc = (org.w3c.dom.Document)result.getNode();
/** * This can be extened to have a map from publicId/systemId * to InputSource. */ public static class Resolver implements EntityResolver { private InputSource source; protected Resolver() { this((InputSource) null); } public Resolver(Document doc) { this(XmlUtil.toString(doc, false)); } public Resolver(String str) { this(new InputSource(new StringReader(str))); } public Resolver(InputSource source) { this.source = source; } public InputSource resolveEntity( String publicId, String systemId) throws SAXException, IOException { return source; } }
DOMResult result = new DOMResult(); unmarshal(((org.w3c.dom.Document)result.getNode()), Docx4jProperties.getProperty("docx4j.model.datastorage.BindingTraverserXSLT.ValidationEventContinue", false))); } catch (UnmarshalException e) { log.error("Problem: " + XmlUtils.w3CDomNodeToString(result.getNode())); throw e;
DOMResult result = new DOMResult(); return unmarshal(((org.w3c.dom.Document)result.getNode()), Docx4jProperties.getProperty("docx4j.model.datastorage.BindingTraverserXSLT.ValidationEventContinue", false)); } catch (UnmarshalException e) { log.error("Problem: " + XmlUtils.w3CDomNodeToString(result.getNode())); throw e;