Example usage for javax.xml.transform.dom DOMSource setNode

List of usage examples for javax.xml.transform.dom DOMSource setNode

Introduction

In this page you can find the example usage for javax.xml.transform.dom DOMSource setNode.

Prototype

public void setNode(Node node) 

Source Link

Document

Set the node that will represents a Source DOM tree.

Usage

From source file:Main.java

public static void writeXml(OutputStream os, Node node, String encoding) throws TransformerException {
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty("indent", "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);

    DOMSource source = new DOMSource();
    source.setNode(node);
    StreamResult result = new StreamResult();
    result.setOutputStream(os);/*from  w  ww . ja v a2s.c  o  m*/

    transformer.transform(source, result);
}

From source file:fr.ece.epp.tools.Utils.java

public static void output(Node node, String filename) {
    TransformerFactory transFactory = TransformerFactory.newInstance();
    try {/*from w w w . ja  va  2s .  c  om*/
        Transformer transformer = transFactory.newTransformer();
        transformer.setOutputProperty("encoding", "utf8");
        transformer.setOutputProperty("indent", "yes");
        DOMSource source = new DOMSource();
        source.setNode(node);
        StreamResult result = new StreamResult();
        if (filename == null) {
            result.setOutputStream(System.out);
        } else {
            result.setOutputStream(new FileOutputStream(filename));
        }
        transformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:org.springframework.oxm.support.AbstractMarshaller.java

/**
 * Template method for handling {@code DOMSource}s.
 * <p>This implementation delegates to {@code unmarshalDomNode}.
 * If the given source is empty, an empty source {@code Document}
 * will be created as a placeholder./*from w w w.j  av  a  2  s.c  o m*/
 * @param domSource the {@code DOMSource}
 * @return the object graph
 * @throws XmlMappingException if the given source cannot be mapped to an object
 * @throws IllegalArgumentException if the {@code domSource} is empty
 * @see #unmarshalDomNode(org.w3c.dom.Node)
 */
protected Object unmarshalDomSource(DOMSource domSource) throws XmlMappingException {
    if (domSource.getNode() == null) {
        domSource.setNode(buildDocument());
    }
    try {
        return unmarshalDomNode(domSource.getNode());
    } catch (NullPointerException ex) {
        if (!isSupportDtd()) {
            throw new UnmarshallingFailureException(
                    "NPE while unmarshalling. " + "This can happen on JDK 1.6 due to the presence of DTD "
                            + "declarations, which are disabled.",
                    ex);
        }
        throw ex;
    }
}