Example usage for javax.xml.transform Transformer transform

List of usage examples for javax.xml.transform Transformer transform

Introduction

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

Prototype

public abstract void transform(Source xmlSource, Result outputTarget) throws TransformerException;

Source Link

Document

Transform the XML Source to a Result.

Usage

From source file:Main.java

public static void writeXmlFile(Document doc, File file)
        throws TransformerConfigurationException, TransformerException {
    Source source = new DOMSource(doc);
    Result result = new StreamResult(file);

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.transform(source, result);
}

From source file:ApplyXPathJAXP.java

static void printNode(Node node) throws Exception {
    if (isTextNode(node)) {
        System.out.println(node.getNodeValue());
    } else {// w  w w. j a va 2  s  . c om
        // Set up an identity transformer to use as serializer.
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.transform(new DOMSource(node), new StreamResult(new OutputStreamWriter(System.out)));
    }

}

From source file:com.twentyn.patentExtractor.Util.java

public static String documentToString(Document doc)
        throws ParserConfigurationException, TransformerConfigurationException, TransformerException {
    StringWriter stringWriter = new StringWriter();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(stringWriter);
    Transformer transformer = getTransformerFactory().newTransformer();
    transformer.transform(source, result);
    return stringWriter.toString();
}

From source file:Main.java

public static String nodeToString(final Node node) {
    final TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer;
    try {/*from ww w.  ja v a  2s  .  c om*/
        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:hoot.services.utils.XmlDocumentBuilder.java

/**
 * //from ww  w.j  av a  2 s  .c o  m
 * 
 * @param node
 * @return
 * @throws TransformerFactoryConfigurationError 
 * @throws TransformerException 
 */
public static String nodeToString(final Node node)
        throws TransformerFactoryConfigurationError, TransformerException {
    StringWriter writer = new StringWriter();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(new DOMSource(node), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

public static void serialize(Node n, StreamResult sr) throws IOException, TransformerException {
    TransformerHandler hd = newTransformerHandler();
    Transformer serializer = hd.getTransformer();

    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");

    DOMSource source = new DOMSource(n);
    serializer.transform(source, sr);
}

From source file:Main.java

public static void buildXmlFile(Document doc, File file) {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    try {/*from  ww  w .  j  a va2s . c o  m*/
        Transformer transformer = tfactory.newTransformer();
        DOMSource source = new DOMSource(doc);

        StreamResult result = new StreamResult(file);
        transformer.setOutputProperty("encoding", "gb2312");
        transformer.transform(source, result);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

static public void outputToStream(Document document, OutputStream outputStream) throws Exception {
    // Use a Transformer for output
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    //    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(outputStream);
    transformer.transform(source, result);

}

From source file:Main.java

public static void createEmptyXML222(String xmlFile) {
    try {// w  w  w .  ja va 2 s. c o  m
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        FileOutputStream fo = new FileOutputStream(xmlFile);
        StreamResult result = new StreamResult(fo);
        transformer.transform(source, result);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

static public void outputToStream(Element document, OutputStream outputStream) throws Exception {
    // Use a Transformer for output
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    //     transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(outputStream);
    transformer.transform(source, result);

}