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 convertToString(Node node) {
    boolean withXMLDeclaration = true;
    String result;/*from   ww w .j a  v  a  2  s.c om*/
    if (withXMLDeclaration) {
        Document document = node.getOwnerDocument();
        DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
        LSSerializer serializer = domImplLS.createLSSerializer();
        result = serializer.writeToString(node);
    } else {
        try {
            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transformer = transFactory.newTransformer();
            StringWriter buffer = new StringWriter();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.transform(new DOMSource(node), new StreamResult(buffer));
            result = buffer.toString();
        } catch (TransformerConfigurationException e) {
            result = "";
        } catch (TransformerException e) {
            result = "";
        }
    }
    return result;
}

From source file:Main.java

public static String xmlParaString(Node doc) {
    DOMSource xmlSource = new DOMSource(doc);

    StringWriter sw = new StringWriter();
    Result result = new StreamResult(sw);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer;

    try {/*w  ww . ja  v  a  2s .c o  m*/
        transformer = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    }

    try {
        transformer.transform(xmlSource, result);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    return sw.toString();
}

From source file:Main.java

public static String sourceToString(Source source) throws IOException {

    try {/*from  www.  java 2  s.c o  m*/
        Transformer trans = transFactory.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        Writer writer = new StringWriter();
        trans.transform(source, new StreamResult(writer));
        writer.flush();
        return writer.toString();
    } catch (TransformerException ex) {
        throw new IOException(ex);
    }
}

From source file:Main.java

public static void transformDOMToFile(Node node, String filePath)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    Source source = new DOMSource(node);
    File file = new File(filePath);
    Result result = new StreamResult(file);

    transformer.transform(source, result);
}

From source file:Main.java

/**
 * Filters an XML document.// w w w  . j  a  v a2 s  .  co  m
 * 
 * @param source the input source for the XML document
 * @param filter the filter
 * 
 * @return an input source for the resulting document
 * 
 * @throws Exception
 */
public static InputSource filterXml(InputSource source, XMLFilter filter) throws Exception {
    // Create filter, which uses a "real" XMLReader but also removes the
    // attributes
    XMLReader reader = XMLReaderFactory.createXMLReader();
    filter.setParent(reader);

    // Create a Transformer
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();

    // Transform the source tree, applying the filter, and store the result
    // tree in a character array
    CharArrayWriter writer = new CharArrayWriter();
    t.transform(new SAXSource(filter, source), new StreamResult(writer));

    // Return a new InputSource using the result tree
    return new InputSource(new CharArrayReader(writer.toCharArray()));
}

From source file:Main.java

public static String toString(Node doc) throws Exception {
    DOMSource domSource = new DOMSource(doc);
    StreamResult result = new StreamResult(new StringWriter());
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = tf.newTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    //serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"users.dtd");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.transform(domSource, result);
    return result.getWriter().toString();
}

From source file:Main.java

public static Node trance(Node context, Transformer t) throws Exception {
    DocumentFragment df = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
            .createDocumentFragment();//from w ww .ja  va  2 s.c o m

    Result result = new DOMResult(df);

    t.transform(new DOMSource(context), result);

    return df;
}

From source file:Main.java

/**
 * Converts a DOM document to an xml String.
 *
 * @param doc DOM document/* w ww .  jav  a2s .  c om*/
 * @return xml String.
 * @throws TransformerException
 */
public static String getStringFromDomDocument(org.w3c.dom.Document doc, org.w3c.dom.Document xslt)
        throws TransformerException {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;
    if (null != xslt) {
        DOMSource dxsltsource = new DOMSource(xslt);
        transformer = tf.newTransformer(dxsltsource);
    } else {
        transformer = tf.newTransformer();
    }
    transformer.transform(domSource, result);
    return writer.toString();
}

From source file:com.thinkbiganalytics.feedmgr.nifi.NifiTemplateParser.java

public static String documentToString(Document doc) throws Exception {

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    String output = writer.getBuffer().toString();
    return output;
}

From source file:Main.java

public static String convertDocumentToString(Document doc)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();

    // Unquote below to remove XML declaration.
    // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    StringWriter writer = new StringWriter();

    transformer.transform(new DOMSource(doc), new StreamResult(writer));

    String output = writer.getBuffer().toString();

    return output;
}