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

public static String toString(Document doc) throws TransformerException {
    StringWriter sw = new StringWriter();
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer transformer = tfactory.newTransformer();
    transformer.transform(new DOMSource(doc), new StreamResult(sw));

    return sw.toString();
}

From source file:Main.java

public static void document2OutputStream(Document document, String encode, OutputStream os)
        throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    DOMSource source = new DOMSource(document);
    transformer.setOutputProperty("encoding", encode);
    transformer.setOutputProperty("indent", "yes");

    transformer.transform(source, new StreamResult(os));
}

From source file:Main.java

public static String writeXMLtoString(org.w3c.dom.Document doc) throws Exception {
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans = transfac.newTransformer();

    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    DOMSource source = new DOMSource(doc);
    trans.transform(source, result);//from ww  w  . ja  v a 2  s.  c o  m
    String xmlString = sw.toString();

    return xmlString;
}

From source file:Main.java

/**
 * /*from   w w w. java2  s.co  m*/
 */
public static Transformer getTransformer() throws TransformerConfigurationException {
    TransformerFactory factory = TransformerFactory.newInstance();

    return factory.newTransformer();
}

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

/**
 * Write a XML document down to a file.//from  w w  w . jav a 2  s .c  o  m
 */
public static void writeXmlToFile(Document document, File file) throws TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(document), new StreamResult(file));
}

From source file:Main.java

public static void writeXmlFile(Document file, String fileName) throws TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    DOMSource source = new DOMSource(file);
    StreamResult result = new StreamResult(new File(fileName));
    transformer.transform(source, result);
}

From source file:Main.java

public static String toXmlString(Document doc) throws Exception {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    writer.flush();//from w  w  w  .j ava  2s.  c o  m

    return writer.toString();
}

From source file:Main.java

public static String xmlToString(Document doc) throws Exception {

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc.getDocumentElement()), new StreamResult(writer));
    String result = writer.toString();
    System.out.println(result);/*from w  ww  .j a v  a2s .  c o m*/
    return result;
}

From source file:Main.java

public static void write(Document doc, Writer writer)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(writer);
    transformer.transform(source, result);
}