Example usage for javax.xml.transform Transformer transform

List of usage examples for javax.xml.transform Transformer transform

Introduction

In this page you can find the example usage for javax.xml.transform Transformer transform.

Prototype

public abstract void transform(Source xmlSource, Result outputTarget) throws TransformerException;

Source Link

Document

Transform the XML Source to a Result.

Usage

From source file:Main.java

public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {//from w  ww .  j av  a  2s  .  co  m
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
        throw new RuntimeException(te);
    }
    return sw.toString();
}

From source file:Main.java

public static int node2Stream(Node node, OutputStream out) throws Exception {
    if (node == null || out == null)
        return -1;

    try {/*from w  ww. j  a v  a 2 s .c  o m*/
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.transform(new DOMSource(node), new StreamResult(out));
        return 0;
    } catch (Exception e) {
        throw e;
    }
}

From source file:Main.java

/**
 * Convert Properties to string//from  w  w w.  j  a va 2  s  .  c  o  m
 *
 * @param props
 * @return xml string
 * @throws IOException
 */
public static String writePropToString(Properties props) throws IOException {
    try {
        org.w3c.dom.Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        org.w3c.dom.Element conf = doc.createElement("configuration");
        doc.appendChild(conf);
        conf.appendChild(doc.createTextNode("\n"));
        for (Enumeration e = props.keys(); e.hasMoreElements();) {
            String name = (String) e.nextElement();
            Object object = props.get(name);
            String value;
            if (object instanceof String) {
                value = (String) object;
            } else {
                continue;
            }
            org.w3c.dom.Element propNode = doc.createElement("property");
            conf.appendChild(propNode);

            org.w3c.dom.Element nameNode = doc.createElement("name");
            nameNode.appendChild(doc.createTextNode(name.trim()));
            propNode.appendChild(nameNode);

            org.w3c.dom.Element valueNode = doc.createElement("value");
            valueNode.appendChild(doc.createTextNode(value.trim()));
            propNode.appendChild(valueNode);

            conf.appendChild(doc.createTextNode("\n"));
        }

        Source source = new DOMSource(doc);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, result);

        return stringWriter.getBuffer().toString();
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:Main.java

public static String toXmlString(Document document) {
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    Transformer transformer;
    try {//from  ww  w.  j av a  2  s  . c o m
        transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(document.getLastChild()), result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return sw.toString();
}

From source file:Main.java

public static void saveDocument(Document document, String path)
        throws TransformerConfigurationException, TransformerFactoryConfigurationError,
        TransformerFactoryConfigurationError, TransformerException, IOException {

    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    DOMSource dom = new DOMSource(document);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", 4);

    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(dom, sr);

    String string = sw.toString();
    FileWriter fw = new FileWriter(new File(path));
    fw.write(string);//from  www .  j  av a2  s.  c o m
    fw.close();
}

From source file:Main.java

/**
 * //from  w w  w  .  j a  v a2 s .  com
 * @param xml
 * @param indent
 * @return pretty formatted xml
 */
public static String prettyFormat(String xml, int indent) {
    try {
        Source xmlInput = new StreamSource(new StringReader(xml));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException(e); // simple exception handling, please review it
    }
}

From source file:Main.java

public static String domNodeToString(Node node) throws TransformerException {
    StringWriter writer = new StringWriter();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(node), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

public static synchronized void deserialize(Source source, Result result, InputStream xsltSource)
        throws TransformerConfigurationException, JAXBException, TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer;
    transformer = factory.newTransformer(new StreamSource(xsltSource));
    transformer.transform(source, result);
}

From source file:Main.java

public static void writeXml(Document dom, OutputStream os) throws TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(dom);
    StreamResult result = new StreamResult(os);
    transformer.transform(source, result);
}

From source file:Main.java

private static String nodeToString(Node node) throws Exception {
    StringWriter sw = new StringWriter();
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
}