Example usage for javax.xml.transform TransformerFactory newInstance

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

Introduction

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

Prototype

public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError 

Source Link

Document

Obtain a new instance of a TransformerFactory .

Usage

From source file:Main.java

/** Returns the entier value of a node (child nodes and text) as a String
 * @param node//w w  w  .  j  av  a2  s . c  o  m
 * @return
 */
public static String nodeChildrenToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        NodeList nl = node.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++)
            t.transform(new DOMSource(nl.item(i)), new StreamResult(sw));
        //      dimes.AgentGuiComm.GUICommunicator.sendLog(Level.WARNING, "", sw.toString());
    } catch (TransformerException te) {
        System.out.println("nodeToString Transformer Exception");
    }
    String test = sw.toString();
    return sw.toString().trim();
}

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

public static File saveToFile(String filename, Document document) throws TransformerException {
    // Prepare the DOM document for writing
    Source source = new DOMSource(document);

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

    // Write the DOM document to the file
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.transform(source, result);// w  ww. ja  v  a 2  s.com

    return file;
}

From source file:Main.java

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

From source file:Main.java

public static String formatDocument2(Document document) throws TransformerException {
    DOMSource domSource = new DOMSource(document);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    StreamResult streamResult = new StreamResult(out);
    TransformerFactory tf = TransformerFactory.newInstance();

    Transformer serializer = tf.newTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.transform(domSource, streamResult);
    return new String(out.toByteArray());
}

From source file:Main.java

public static String docToString(Document doc, boolean formated) {

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {//from w w  w  . j  a v a2 s . c  om
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
        return null;
    }

    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    if (formated) {
        // linefeed formatting
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    } else {
        // remove xml header
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    }
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);

    DOMSource domSource = new DOMSource(doc);
    try {
        transformer.transform(domSource, sr);
    } catch (TransformerException e) {
        e.printStackTrace();
        return null;
    }
    return sw.toString();
}

From source file:Main.java

public static String prettyPrint(DOMSource domSource) {
    try {//w  ww  .  ja  v  a 2 s  . c  om
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        StreamResult streamResult = new StreamResult(new StringWriter());
        transformer.transform(domSource, streamResult);

        return streamResult.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException("Error while pretty printing xml", e);
    }
}

From source file:Main.java

/**
 * Writes a {@link Document} as text to the given {@link Writer}.
 * @param doc the document to output//from w w  w.  jav a2 s .com
 * @param out the writer to output to
 */
public static void writeDocument(Document doc, Writer out) {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {
        transformer = transformerFactory.newTransformer();
    } catch (final TransformerConfigurationException e) {
        throw new RuntimeException(e); // TODO proper exception handling
    }
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(out);

    try {
        transformer.transform(source, result);
    } catch (final TransformerException e) {
        throw new RuntimeException(e); // TODO proper exception handling
    }
}

From source file:Main.java

/**
 * Creates XML from W3C Document from the xml.
 *
 * @param document the xml that needs to be converted.
 * @return the XML.// w  w w  . j av a 2s. c  om
 * @throws Exception is there is some error during operation.
 */
public static String createXml(Document document) throws Exception {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    StringWriter stringWriter = new StringWriter();
    StreamResult result = new StreamResult(stringWriter);
    DOMSource source = new DOMSource(document);
    transformer.transform(source, result);
    return stringWriter.toString();
}

From source file:Main.java

/**
 * Creates a {@link String} out of a {@link Node}
 * //from  w  ww .  j  a  va2s  .c  o  m
 * @param node
 *            never <code>null</code>
 * @return the node as string, never <code>null</code>
 * @throws Exception
 */
public static String asString(Node node) throws Exception {
    StringWriter writer = new StringWriter();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(new DOMSource(node), new StreamResult(writer));
    return writer.toString();
}