Example usage for javax.xml.transform Transformer setOutputProperty

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

Introduction

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

Prototype

public abstract void setOutputProperty(String name, String value) throws IllegalArgumentException;

Source Link

Document

Set an output property that will be in effect for the transformation.

Usage

From source file:Main.java

public static String getDomDocumentAsXml(org.w3c.dom.Document domDocument) {
    try {//ww  w. j a v  a  2 s  . c o m
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult xmlOutput = new StreamResult(new StringWriter());
        transformer.transform(new DOMSource(domDocument), xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * returns an XML string./*ww  w .jav a  2  s.c  o  m*/
 *
 * @param pNode         Node XML Document node
 * @return              String XML string
 */
public static String getXML(Node pNode) {
    String retString = null;
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.transform(new DOMSource(pNode), new StreamResult(out));
        retString = out.toString();
        out.close();
    } catch (Exception ex) {
    }
    return retString;
}

From source file:Main.java

public static Transformer generateTransformer(boolean isOmitXmlDeclaration, boolean isIndent)
        throws TransformerConfigurationException {
    Transformer transformer = generateTransformer(isOmitXmlDeclaration);
    if (isIndent) {
        transformer.setOutputProperty("indent", "yes");
    } else {/*from  w  w w.j a va  2 s .  c  o m*/
        transformer.setOutputProperty("indent", "no");
    }
    return transformer;
}

From source file:Main.java

public static void writeXmlFile(Document doc, File file, boolean indent, String encoding)
        throws TransformerFactoryConfigurationError, TransformerException {
    // Prepare the DOM document for writing
    Source source = new DOMSource(doc);

    // Prepare the output file
    Result result = new StreamResult(file);

    Transformer xformer = TransformerFactory.newInstance().newTransformer();

    xformer.setOutputProperty(OutputKeys.ENCODING, encoding);

    if (indent) {
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    }/*  ww  w  .  ja  va 2  s  .  co m*/

    xformer.transform(source, result);

}

From source file:Main.java

/**
 * returns an XML string.//from w  ww  .ja va  2  s.  c  o m
 *
 * @param pDocument         Document XML DOM document
 * @return                  String XML string
 */
public static String getXML(Document pDocument) throws Exception {
    String retString = null;
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.transform(new DOMSource(pDocument), new StreamResult(out));
        retString = out.toString();
        out.close();
    } catch (Exception ex) {
        throw new Exception(ex.getMessage());
    }
    return retString;
}

From source file:Main.java

public static void transformDocument(final Document doc, final String xsl, final String targetFile) {
    try {/*from ww  w  . ja  va 2 s . c  om*/
        final Source source = new DOMSource(doc);

        // Creation of the output file
        final File file = new File(targetFile);
        final Result result = new StreamResult(file);

        // configuration of the transformer
        final TransformerFactory factoryT = TransformerFactory.newInstance();
        final StreamSource stylesource = new StreamSource(xsl);
        Transformer transformer;

        transformer = factoryT.newTransformer(stylesource);
        transformer.setOutputProperty(OutputKeys.METHOD, "text");

        // Transformation
        transformer.transform(source, result);
    } catch (final TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (final TransformerException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void transformDocument(Document doc, String xsl, String targetFile) {
    try {/* www . ja v a2  s.  c  o  m*/
        Source source = new DOMSource(doc);

        // Creation of the output file
        File file = new File(targetFile);
        Result result = new StreamResult(file);

        // configuration of the transformer
        TransformerFactory factoryT = TransformerFactory.newInstance();
        StreamSource stylesource = new StreamSource(xsl);
        Transformer transformer;

        transformer = factoryT.newTransformer(stylesource);
        transformer.setOutputProperty(OutputKeys.METHOD, "text");

        // Transformation
        transformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 * Helper to turn on indentation for a {@link Transformer} that works correctly for
 * both Saxon and Xalan.//from w  ww.j av a2 s  . c om
 * 
 * @param transformer {@link Transformer} to configure
 * @param indent required indentation, where 0 or more provides indentation and negative
 *   numbers turns indentation off.
 */
public static void setIndentation(Transformer transformer, int indent) {
    if (indent >= 0) {
        String indentString = String.valueOf(indent);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        /* Set custom properties for both Saxon and Xalan at once.
         * This appears safe to do without having to check the underlying processor.
         */
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", indentString);
        transformer.setOutputProperty("{http://saxon.sf.net/}indent-spaces", indentString);
    } else {
        transformer.setOutputProperty(OutputKeys.INDENT, "no");
    }
}

From source file:Main.java

private static String getIndented(Document aDoc) {
    String outputXml = "";
    try {/*from   ww  w  . ja  va2s. c  o  m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        //      tf.setAttribute("indent-number", new Integer(4));
        Transformer transformer;
        transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(aDoc);
        transformer.transform(source, result);
        outputXml = result.getWriter().toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return outputXml;
}

From source file:br.gov.lexml.parser.documentoarticulado.LexMLUtil.java

public static String xmlToString(Document doc) {
    StringWriter sw = new StringWriter();
    try {//from  w w  w.  java 2  s .com
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING);
        transformer.transform(new DOMSource(doc), new StreamResult(sw));
    } catch (TransformerException e) {
        throw new IllegalArgumentException(e);
    }
    return sw.toString();
}