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

public static void saveXml(Document modDoc, String path) throws TransformerException, IOException {
    Writer writer = null;/*ww w .  j a  v a 2s. c  o  m*/
    try {
        TransformerFactory tranFactory = TransformerFactory.newInstance();
        Transformer aTransformer = tranFactory.newTransformer();
        Source src = new DOMSource(modDoc);
        writer = getFileWriter(path);
        Result dest = new StreamResult(writer);

        aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
        aTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
        aTransformer.transform(src, dest);
    } catch (TransformerException e) {
        throw e;
    } finally {

        writer.close();
    }

}

From source file:Main.java

public static String XML2String(Node node) {
    try {//from  w  ww .  j a v a 2s.  c  o m
        StringWriter writer = new StringWriter();
        Transformer trans = TransformerFactory.newInstance().newTransformer();
        trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        Source source = new DOMSource(node);
        Result result = new StreamResult(writer);
        trans.transform(source, result);
        return writer.toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void save(Document document) throws TransformerConfigurationException, TransformerException,
        TransformerFactoryConfigurationError, SAXException, IOException, ParserConfigurationException {
    TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document),
            new StreamResult(PATH));
}

From source file:Main.java

/**
 * Stores an XMl document into a file.//from   w w  w  .  jav a  2 s  .com
 *
 * @param doc xml document to be stored
 * @param location absolute path where to write down the document
 * @throws TransformerException If an unrecoverable error occurs during the
 * course of the transformation.
 */
public static void save(Document doc, String location) throws TransformerException {
    doc.normalize();
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult streamResult = new StreamResult(new File(location));
    transformer.transform(source, streamResult);
}

From source file:Main.java

/**
 * Serialize XML from a {@link Document} to an OutputStream.
 * @param doc//from   w  w w .  j a va  2s  .c o m
 * @param out
 */
public static void serialiseXml(Document doc, Writer out) {
    DOMSource domSource = new DOMSource(doc.getDocumentElement());
    StreamResult streamResult = new StreamResult(out);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = null;
    try {
        serializer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
    }
    serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "users.dtd");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    try {
        serializer.transform(domSource, streamResult);
    } catch (TransformerException e) {
    }
}

From source file:Main.java

/**
 * DOM to string.//w ww.ja va 2  s  .co  m
 *
 * @param doc
 *            the doc
 * @return the string
 */
/*
 * from:
 * http://www.journaldev.com/71/utility-java-class-to-format-xml-document
 * -to-xml-string-and-xml-to-document
 */
public static String DOMToString(Document doc) {
    String xmlString = "";
    if (doc != null) {
        try {
            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            trans.setOutputProperty(OutputKeys.INDENT, "yes");
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);
            xmlString = sw.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return xmlString;
}

From source file:Main.java

private static void WriteXMLFile(Document doc, OutputStreamWriter w, String encoding) {
    try {//from   w w w .jav  a 2 s  .c  o m
        Source source = new DOMSource(doc);
        Result ret = new StreamResult(w);
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        xformer.transform(source, ret);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();

    }

}

From source file:Main.java

/**
 * Returns the String-Representation of the given DOM-Node as well-formed DOM-Document.
 *
 * @param node DOM-Node to print//from w  w  w . j  ava 2 s .  com
 * @param indent if true resulting XML is endented
 * @return <code>String</code> - Node as XML-String
 * @throws Exception on error
 */
public static String domNode2String(Node node, boolean indent) throws Exception {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");

    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(node);
    transformer.transform(source, result);

    String xmlString = result.getWriter().toString();

    return xmlString;
}

From source file:Main.java

/**
 * Save document to a file//from w  w  w.  j  av  a2s . c  o  m
 *
 * @param document : Document to be saved
 * @param fileName : Represent file name to save
 * @throws Exception
 */
public static void saveDocumentTo(Document document, String fileName) throws Exception {
    File encryptionFile = new File(fileName);
    try (FileOutputStream fOutStream = new FileOutputStream(encryptionFile)) {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(fOutStream);
        transformer.transform(source, result);
    }
}

From source file:Main.java

static void serialize(Document document, Writer writer) {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", new Integer(4));
    try {/*from  www.  java  2 s.  c o m*/
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult result = new StreamResult(writer);
        transformer.transform(new DOMSource(document), result);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}