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 void writeXmlFile(Document doc, String fileFullPath)
        throws TransformerFactoryConfigurationError, TransformerException {
    Source source = new DOMSource(doc);
    File file = new File(fileFullPath);
    Result result = new StreamResult(file);
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(source, result);
}

From source file:Main.java

/**
 * Process a w3c XML document into a Java String.
 * /*w ww. ja  v a2 s.  c  o m*/
 * @param outputDoc
 * @return
 */
public static String printToString(Document doc) {
    try {
        doc.normalizeDocument();
        DOMSource source = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(source, result);
        return writer.toString();
    } catch (Exception e) {
        // e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Converts XML document into the string.
 * @param document XML/*  www  .j av  a  2 s . c  o m*/
 * @return XML as string
 * @throws TransformerException 
 */
public static String toString(Document document) throws TransformerException {
    DOMSource domSource = new DOMSource(document);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    return writer.toString();
}

From source file:Main.java

public static String nodeToString(Node node) throws TransformerException {
    StringWriter writer = new StringWriter();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(new DOMSource(node), new StreamResult(writer));
    return writer.toString().replaceAll("\\<\\?xml(.+?)\\?\\>", "").trim();
}

From source file:Main.java

public static void serialize(Document document, File targetFile) throws IOException {
    try {/*from ww w .ja v a 2 s. c o m*/
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.transform(new DOMSource(document), new StreamResult(targetFile));
    } catch (TransformerException e) {
        throw new IOException(e);
    }
}

From source file:Main.java

public static void ElementToStream(Element element, OutputStream out) {
    try {/*from w ww  .  j  a  va 2  s  .c o m*/
        DOMSource source = new DOMSource(element);
        StreamResult result = new StreamResult(out);
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        transformer.transform(source, result);
    } catch (Exception ex) {
    }
}

From source file:Main.java

public static String NodeToStr(Node node) throws Exception, TransformerFactoryConfigurationError {
    StringWriter sw = new StringWriter();
    Transformer serializer = TransformerFactory.newInstance().newTransformer();
    serializer.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

public static String xmlToString(Document doc) {
    try {/*from   w ww .j a  v  a  2 s .  c o m*/
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (TransformerException ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String nodeToString(Node node) throws TransformerException {
    StringWriter sw = new StringWriter();
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

private static void writeToXmlFile(Document doc, String xmlFilePath)
        throws FileNotFoundException, TransformerException {
    TransformerFactory tffactory = TransformerFactory.newInstance();
    Transformer tf = tffactory.newTransformer();
    tf.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(xmlFilePath)));
}