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 String prettifyXmlString(String uglyXml) {
    Transformer transformer = null;
    try {/*w  ww.  j  av  a  2  s. co m*/
        transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    }
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    //initialize StreamResult with File object to save to file
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = null;
    try {
        source = new DOMSource(string2Document(uglyXml));
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        transformer.transform(source, result);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    String xmlString = result.getWriter().toString();
    return xmlString;
}

From source file:Main.java

public static String transText(Node doc) throws Exception {
    TransformerFactory transfactory = TransformerFactory.newInstance();
    Transformer transformer = transfactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    Source source = new DOMSource(doc);
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    transformer.transform(source, result);
    return sw.toString();
}

From source file:Main.java

public static void saveXML(Document paramDocument, File paramFile) throws TransformerException {
    TransformerFactory localTransformerFactory = TransformerFactory.newInstance();
    Transformer localTransformer = localTransformerFactory.newTransformer();
    localTransformer.setOutputProperty("indent", "yes");
    StreamResult localStreamResult = new StreamResult(paramFile);
    DOMSource localDOMSource = new DOMSource(paramDocument);
    localTransformer.transform(localDOMSource, localStreamResult);
}

From source file:Main.java

/**
 * output XML data in a file/*  w ww.  j  a v a 2 s  .c  o m*/
 *
 * @param doc
 * @param path
 */
public static void buildXmlFile(Document doc, String path) {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    try {
        Transformer transformer = tfactory.newTransformer();
        DOMSource source = new DOMSource(doc);

        StreamResult result = new StreamResult(new File(path));
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("encoding", "UTF-8");
        transformer.transform(source, result);

    } catch (TransformerConfigurationException e) {
        //TODO
    } catch (TransformerException e) {

    }
}

From source file:Main.java

public static String convertDocumentToString(Document dom) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;/* ww  w  . j  a v  a2s .co  m*/
    try {
        transformer = tf.newTransformer();
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(dom), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        return output;
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String xmlToString(Document doc) {
    try {//from  w ww  .ja va2 s  . co m
        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));
        String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
        return output;
    } catch (TransformerException exc) {
        throw new RuntimeException("Unable to convert XML to String");
    }
}

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 String nodeToString(final Node node) {
    final TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer;/*from   w w  w. j  av  a2s.  co m*/
    try {
        transformer = transFactory.newTransformer();
        final StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(new DOMSource(node), new StreamResult(buffer));
        buffer.append("\n");
        return buffer.toString();
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String transform(String xml, File stylesheet) throws TransformerException {
    StringWriter writer = new StringWriter();
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));
    transformer.transform(new StreamSource(xml), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

public static void printDocument(Node doc, OutputStream out) {

    try {//from  w  w w  . j  a va 2 s.  c o  m
        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.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

        transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
    } catch (Exception e) {
        e.printStackTrace();
    }
}