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 String writeXmlToStream(Document doc, Result streamResult) {
    String result = "";
    try {//www .  ja  v a 2  s . c om
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        // Write the DOM document to the file
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer xformer = tFactory.newTransformer();
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        xformer.transform(source, streamResult);
    } catch (TransformerConfigurationException e) {
    } catch (TransformerException e) {
    }
    return result;
}

From source file:Main.java

public static String printNode(Node node) {
    String result = "";
    try {/*w w  w .  j  av  a 2 s .c  o  m*/
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty("method", "xml");
        StringWriter sw = new StringWriter();
        DOMSource source = new DOMSource(node);
        transformer.transform(source, new StreamResult(sw));
        result = sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static void saveDocumentToFile(Document xmlDoc, File file)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    DOMSource source = new DOMSource(xmlDoc);
    StreamResult result = new StreamResult(file);
    transformer.transform(source, result);
}

From source file:Main.java

public static String getStringFromElement(Element doc) throws TransformerException {

    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    // transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    // transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(domSource, result);
    writer.flush();/*from   w w w  .j av a  2 s .  c  o m*/
    return writer.toString();
}

From source file:Main.java

public static void removeHandset(String file, String name) throws Exception {
    DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder dombuilder = domfac.newDocumentBuilder();
    FileInputStream is = new FileInputStream(file);

    Document doc = dombuilder.parse(is);
    NodeList devices = doc.getElementsByTagName("devices");
    NodeList nodeList = doc.getElementsByTagName("device");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node deviceNode = nodeList.item(i);
        if (deviceNode.getTextContent().equals(name)) {
            devices.item(0).removeChild(deviceNode);
        }//from w  ww. ja  v  a  2  s  .co m
    }

    //save
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    Properties props = t.getOutputProperties();
    props.setProperty(OutputKeys.ENCODING, "GB2312");
    t.setOutputProperties(props);
    DOMSource dom = new DOMSource(doc);
    StreamResult sr = new StreamResult(file);
    t.transform(dom, sr);
}

From source file:Main.java

/**  This method dumps out a dom document to an output stream.
 *   Header information is turned off.//from   w w  w .ja v  a2s . co  m
 *
 *   @param doc Dom Document
 *   @param os existing outputstream you wish to write the document to.
 */
public static void DOMtoOutputStream(Document doc, OutputStream os) {
    try {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        DOMSource src = new DOMSource(doc);
        StreamResult result = new StreamResult(os);

        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        //transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.transform(src, result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void createEmptyXML222(String xmlFile) {
    try {/*  w ww. j  a va  2 s .c om*/
        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

private static void transfer(Document doc, Result streamResult)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {

    Source source = new DOMSource(doc);
    TransformerFactory xformFactory = TransformerFactory.newInstance();
    Transformer idTransform = xformFactory.newTransformer();
    idTransform.transform(source, streamResult);
}

From source file:Main.java

/** 
 * Returns the XML string representation of a Node.
 * @param node Node to convert into XML string. 
 * @param omitXmlDeclaration <tt>true</tt> to remove the XML declaration from the string. 
 * @return The XML string representation of the input node. 
 * @throws TransformerConfigurationException 
 * @throws TransformerException /*from w  ww. j a v  a 2  s .  c om*/
 */
public static String getXMLString(Node node, boolean omitXmlDeclaration)
        throws TransformerConfigurationException, TransformerException {
    StringWriter writer = new StringWriter();
    DOMSource domSource = new DOMSource(node);
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = tf.newTransformer();
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
    serializer.transform(domSource, result);
    return (writer.toString());
}

From source file:Main.java

public static String sourceToXMLString(Source result) {

    String xmlResult = null;//from ww  w .  j a  v a2  s .  c om
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        //transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        StringWriter out = new StringWriter();
        StreamResult streamResult = new StreamResult(out);
        transformer.transform(result, streamResult);
        xmlResult = streamResult.getWriter().toString();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return xmlResult;
}