Example usage for javax.xml.transform TransformerFactory newTransformer

List of usage examples for javax.xml.transform TransformerFactory newTransformer

Introduction

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

Prototype

public abstract Transformer newTransformer() throws TransformerConfigurationException;

Source Link

Document

Create a new Transformer that performs a copy of the Source to the Result .

Usage

From source file:Main.java

/**
 * @param indentation If greater than zero, then the XML will be indented by the value specified
 *//*www  .j av a  2s  .  co  m*/
private static Transformer getXMLidentityTransformer(int indentation) throws Exception {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer t = factory.newTransformer(); // identity transform
    t.setOutputProperty(OutputKeys.INDENT, (indentation != 0) ? YES : NO);
    t.setOutputProperty(OutputKeys.METHOD, "xml"); //xml, html, text
    t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "" + indentation);
    return t;
}

From source file:Main.java

/**
 * Transform a org.w3c.dom.Node to a String
 * @param node - the Node/*from w  w  w  .j  a v  a 2  s  . c o m*/
 * @param omitXmlDeclaration - omit XML declaration
 * @param prettyPrint - apply indentation
 * @return the String result
 * @throws TransformerException
 */
public static String elementToString(Node node, boolean omitXmlDeclaration, boolean prettyPrint) {
    String result = null;
    try {
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        StringWriter buffer = new StringWriter();
        if (omitXmlDeclaration) {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }
        if (prettyPrint) {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        }
        transformer.transform(new DOMSource(node), new StreamResult(buffer));
        result = buffer.toString();
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:Main.java

public static String getString(Node doc) {
    StringWriter sw = new StringWriter();

    try {//from w  ww  .  j  a  v a2 s . c  o  m
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        // create string from xml tree
        trans.transform(new DOMSource(doc), new StreamResult(sw));
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return sw.toString();
}

From source file:Main.java

public static String getDOMString(Document doc) {

    String s = null;/*www  .  j  a  va 2  s  . c o  m*/
    final TransformerFactory tfactory = TransformerFactory.newInstance();
    try {
        final Transformer xform = tfactory.newTransformer();
        final Source src = new DOMSource(doc);
        final StringWriter writer = new StringWriter();
        final Result result = new StreamResult(writer);
        xform.transform(src, result);
        s = writer.toString();
    } catch (final Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return s;
}

From source file:Main.java

public static byte[] serializeFragment(Document root)
        throws TransformerException, UnsupportedEncodingException {
    Source source = new DOMSource((Node) root);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");

    return serializeXML(transformer, source);
}

From source file:Main.java

public static String documentToXmlString(Document document) {
    DOMSource domSource = new DOMSource(document);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;/* w  w w .  ja  v  a2 s .  com*/
    try {
        transformer = tf.newTransformer();
        transformer.transform(domSource, result);
    } catch (TransformerException e) {
        throw new RuntimeException(e.getMessage());
    }
    return writer.toString();
}

From source file:Main.java

public static void createXML222(String xmlFile, String xpath, String element, String data) {
    try {/*  w  w w.j  a v  a 2 s . c om*/
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();
        Element rootElement = document.createElement(xpath);
        document.appendChild(rootElement);
        for (int i = 1; i <= 1; i++) {
            Element em = document.createElement(element);
            em.appendChild(document.createTextNode(data));
            rootElement.appendChild(em);
        }
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        FileOutputStream fo = new FileOutputStream(xmlFile);
        StreamResult result = new StreamResult(fo);
        transformer.transform(source, result);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

/**
 * Method to convert document to string.
 * /* www .  ja v a2 s  . c  om*/
 * @param doc
 * @return document content as string
 */
public static String doc2String(Document doc) {
    try {
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(new DOMSource(doc), new StreamResult(sw));
        return sw.toString();
    } catch (Exception ex) {
        throw new RuntimeException("Error converting to String", ex);
    }
}

From source file:Main.java

/**
 * Writes the given document to the given writer.
 *
 * @param document the document to write
 * @param writer receives the written document
 * @param indent number of spaces to indent, null means don't indent
 *///from  ww w .j av a  2  s  .c om
public static void writeDocument(Document document, Writer writer, Integer indent) {
    TransformerFactory tf = TransformerFactory.newInstance();
    try {
        Transformer trans = tf.newTransformer();
        if (indent != null) {
            trans.setOutputProperty(OutputKeys.INDENT, "yes");
            trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
        }
        trans.transform(new DOMSource(document), new StreamResult(writer));
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

private static void save(Document doc, Result result) {
    //--- Write the XML document in XML file
    try {//  www  . j  a  v a 2 s.  c  o  m
        Source source = new DOMSource(doc);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();

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

        //--- Transform the DOM document into XML file 
        transformer.transform(source, result);
    } catch (TransformerException e) {
        throw new RuntimeException("XML error : Cannot save : TransformerException", e);
    } catch (TransformerFactoryConfigurationError e) {
        throw new RuntimeException("XML error : Cannot save : TransformerFactoryConfigurationError", e);
    }
}