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 String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {//from   ww w . j ava 2 s.  co m
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
        System.err.println("nodeToString Transformer Exception");
    }
    return sw.toString();
}

From source file:Main.java

public static String nodeListToString(NodeList nodeList) throws TransformerException {
    StringWriter stringWriter = new StringWriter();
    for (int i = 0; i < nodeList.getLength(); ++i) {
        Node node = nodeList.item(i);
        if (node instanceof Element) {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.transform(new DOMSource(node), new StreamResult(stringWriter));
        } else {//  w w  w  .j a v  a2s .c  o m
            stringWriter.append(node.getTextContent());
        }
    }
    return stringWriter.toString();
}

From source file:Main.java

/**
 * Updates existing file with Document object
 * // ww w.  ja va  2s .c  o  m
 * @param doc
 *            xml document
 * @param file
 *            xml file to update
 * @throws TransformerFactoryConfigurationError
 */
public static void updateXMLFile(Document doc, File file) throws TransformerFactoryConfigurationError {
    try {
        DOMSource source = new DOMSource(doc);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult result = new StreamResult(file);
        transformer.transform(source, result);
    } catch (Exception e) {
        throw new RuntimeException("Exception while saving message to xml file", e);
    }
}

From source file:Main.java

/**
 * Serialize an XML document into a String (for debug purpose only!). the
 * returned String could contains an error message instead if a problem as
 * occurred during the serialization. This method is intended for debug /
 * trace purpose because you really don't need to serialize XML at all in
 * your code unless your planing to output it to a file. In this case, use
 * {@link XmlHelper#writeXmlToFile(Document, File)} instead.
 *///w  w  w.j a v  a2s  .  com
public static String dumpXml(Document document) {
    TransformerFactory factory = TransformerFactory.newInstance();
    try {
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (TransformerException e) {
        return "XML dump failed: " + e.getMessage();
    }
}

From source file:Main.java

public static String xmlToString(Document doc) {
    try {/*from  w  w  w  .j  av  a2s.c  om*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        return result.getWriter().toString();
    } catch (TransformerException ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void saveDoc(Document doc, String fileName) {
    try {//from   w  ww. ja  v  a  2  s. c o m
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);

        StreamResult result = new StreamResult(new File(fileName));
        transformer.transform(source, result);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

private static final String transform(Transformer transformer, Node element) throws TransformerException {
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(element);
    transformer.transform(source, result);

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

From source file:Main.java

public static void dumpElement(Logger log, Element elem) {

    try {/*from www  .  ja v a  2 s.  c  o  m*/
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(new DOMSource(elem), new StreamResult(buffer));

        log.debug(buffer.toString());
    } catch (TransformerFactoryConfigurationError | IllegalArgumentException | TransformerException ex) {
        log.error("Failed to dump element", ex);
    }
}

From source file:Main.java

public static org.w3c.dom.Document writeToFile(String xmlContent, String path) {
    System.out.println("This is the path " + path);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;/*  w  w w . j  a v  a2  s .co m*/
    try {
        builder = factory.newDocumentBuilder();
        org.w3c.dom.Document doc = builder.parse(new InputSource(new StringReader(xmlContent)));

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(path));

        transformer.transform(source, result);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

/**
 * /*w  ww .  j a va  2  s . c o  m*/
 * @param fileName : output file
 * @param document :  xml document
 * @throws TransformerFactoryConfigurationError
 * @throws TransformerConfigurationException
 * @throws TransformerException
 */
private static void writeDocIntoFile(final String fileName, final Document document)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
    // update the XML file
    final TransformerFactory transformerFactory = TransformerFactory.newInstance();
    final Transformer transformer = transformerFactory.newTransformer();
    final DOMSource source = new DOMSource(document);
    final StreamResult result = new StreamResult(new File(fileName));
    transformer.transform(source, result);
}