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 void sendXml(String result, int operationN) throws Exception {
    String filepath = "file.xml";
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(filepath);

    Node tonode = doc.getElementsByTagName("command").item(0);
    Element res = doc.createElement("result");
    res.setTextContent(result);//  ww w  .ja v a  2 s  .  c om
    tonode.appendChild(res);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result1 = new StreamResult(new File("file2.xml"));

    StreamResult result3 = new StreamResult(System.out);
    transformer.transform(source, result1);
}

From source file:Main.java

public static void printXml(Document doc, Writer writer) throws Exception {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
}

From source file:Main.java

public static String encodeBase64(Element elm) throws Exception {
    ByteArrayOutputStream ostream = new ByteArrayOutputStream();
    TransformerFactory transFac = TransformerFactory.newInstance();
    Transformer trans = transFac.newTransformer();
    trans.transform(new DOMSource(elm), new StreamResult(ostream));
    return (new String(Base64.encodeBase64(ostream.toByteArray(), true)));
}

From source file:Main.java

public static String XMLToString(Document doc) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

public static Transformer getTransformer() throws TransformerConfigurationException {
    if (trans == null) {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        trans = tFactory.newTransformer();
    }//from  w ww .ja va2 s.c o m
    return trans;
}

From source file:Main.java

public static void print(Node dom) {
    try {/*from w w  w .ja  va 2  s.  c  o m*/
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer t = factory.newTransformer();
        StringWriter sw = new StringWriter();
        t.transform(new DOMSource(dom), new StreamResult(sw));
        System.out.println(sw.toString());
    } catch (Exception ex) {
        System.out.println("Could not print XML document");
    }
}

From source file:Main.java

public static void serializeDocument(Document doc, OutputStream outputStream) throws Exception {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer serializer = tfactory.newTransformer();
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    serializer.transform(new DOMSource(doc), new StreamResult(outputStream));
}

From source file:Main.java

public static void document2File(Document document, String encode, File file) 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(file));
}

From source file:Main.java

public static String convertDocumentToString(Document doc) throws IOException, TransformerException {
    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);
    return (writer.toString());
}

From source file:Main.java

public static String newStringFromDocument(Document doc)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty("omit-xml-declaration", "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
    return output;
}