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:ApplyXPathJAXP.java

static void printNode(Node node) throws Exception {
    if (isTextNode(node)) {
        System.out.println(node.getNodeValue());
    } else {//w  w  w.j av a2 s . c o m
        // Set up an identity transformer to use as serializer.
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.transform(new DOMSource(node), new StreamResult(new OutputStreamWriter(System.out)));
    }

}

From source file:Main.java

public static String nodeToString(final Node node) {
    final TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer;
    try {//from w w w.  j a  va2  s  .c  o m
        transformer = transFactory.newTransformer();
        final StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(new DOMSource(node), new StreamResult(buffer));
        buffer.append("\n");
        return buffer.toString();
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String convertToString(Node node) {
    boolean withXMLDeclaration = true;
    String result;/*  ww w.j  av  a  2s .  co m*/
    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 void writeXML(String OUTPUT_XML_FILE, org.w3c.dom.Document xmlDoc) {
    try {// w ww .j  ava  2  s  .  c  o  m
        javax.xml.transform.Source source = new DOMSource(xmlDoc);
        System.out.println("writing File: " + OUTPUT_XML_FILE);
        // Prepare the output file
        File file = new File(OUTPUT_XML_FILE);
        Result result = new StreamResult(file);
        // Write the DOM document to the file
        javax.xml.transform.Transformer xformer = javax.xml.transform.TransformerFactory.newInstance()
                .newTransformer();
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        xformer.transform(source, result);
    } catch (Exception e) {
        System.err.println("Exception in writing file:" + OUTPUT_XML_FILE);
        e.printStackTrace();
    }
}

From source file:Main.java

private static boolean updateXML(Document document, String path) throws TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();

    // out put encoding.
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    DocumentType type = document.getDoctype();
    if (type != null) {
        System.out.println("doctype -->" + type.getPublicId());
        transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, type.getPublicId());
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, type.getSystemId());
    }/*from w  ww.j a v  a 2  s . co m*/

    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(new File(path));
    transformer.transform(source, result);

    transformer.reset();
    return true;
}

From source file:Main.java

/**
 * Helper to turn on indentation for a {@link Transformer} that works correctly for
 * both Saxon and Xalan./*w w w.  j a va2  s .c  o m*/
 *
 * @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(final Transformer transformer, final int indent) {
    if (indent >= 0) {
        final 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

public static final String indenting(Node rootNode) throws XPathExpressionException, TransformerException {

    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", rootNode,
            XPathConstants.NODESET);

    for (int i = 0; i < nodeList.getLength(); ++i) {
        Node node = nodeList.item(i);
        node.getParentNode().removeChild(node);
    }/*from   w  w  w  .j  av a  2  s  . c o m*/

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);

    transformer.transform(new DOMSource(rootNode), streamResult);

    return stringWriter.toString();
}

From source file:Main.java

public static void writeXml(Document doc, File file) {
    try {/*from  w w w  .jav a 2s .com*/
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

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

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();

        try {
            xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        } catch (IllegalArgumentException e) { // ignore
        }

        xformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
        throw new IllegalStateException(e);
    } catch (TransformerException e) {
        throw new IllegalStateException(e);
    }
}

From source file:ApplyXPathJAXP.java

static void printNodeList(NodeList nodelist) throws Exception {
    Node n;/*from   w w w. j a va2s.  c om*/

    // Set up an identity transformer to use as serializer.
    Transformer serializer = TransformerFactory.newInstance().newTransformer();
    serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    for (int i = 0; i < nodelist.getLength(); i++) {
        n = nodelist.item(i);
        if (isTextNode(n)) {
            // DOM may have more than one node corresponding to a 
            // single XPath text node.  Coalesce all contiguous text nodes
            // at this level
            StringBuffer sb = new StringBuffer(n.getNodeValue());
            for (Node nn = n.getNextSibling(); isTextNode(nn); nn = nn.getNextSibling()) {
                sb.append(nn.getNodeValue());
            }
            System.out.print(sb);
        } else {
            serializer.transform(new DOMSource(n), new StreamResult(new OutputStreamWriter(System.out)));
        }
        System.out.println();
    }
}

From source file:Main.java

public static void writeXml(Document doc, OutputStream outputStream) {
    try {/*from w w  w. j  a v  a2 s .co  m*/
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

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

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        try {
            xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
        } catch (IllegalArgumentException e) { // ignore
        }

        try {
            xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        } catch (IllegalArgumentException e) { // ignore
        }

        xformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
        throw new IllegalStateException(e);
    } catch (TransformerException e) {
        throw new IllegalStateException(e);
    }
}