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

/**
 * write out an XML file//from   w w  w.  ja va 2  s .c o m
 * 
 * @param doc
 * @param os
 * @throws TransformerException 
 * @throws IOException 
 */
public static void writeXML(Document doc, OutputStream os) throws TransformerException, IOException {
    // write out xml file
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

    //indent XML properly
    //formatXML(doc,doc.getDocumentElement(),"  ");

    //normalize document
    doc.getDocumentElement().normalize();

    //write XML to file
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(os);

    transformer.transform(source, result);
    os.close();
}

From source file:Main.java

public static void saveDoc(Document doc, String fileName) {
    try {/*from  ww  w  .ja v  a 2s  .  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

public static void printDocument(Document doc, OutputStream out, boolean prettyPrint)
        throws IOException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

    if (prettyPrint) {
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    }/*ww w. java2  s . c  o  m*/

    transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}

From source file:Main.java

/**
 * Transform a DOM Node to String.// ww w  .  j av  a 2s  .c  om
 * @param node The Node to be transformed.
 * @return a String representation.
 * @throws ParserConfigurationException Parser confiuration exception
 * @throws TransformerException Transformer exception
 */
public static String toString(final Node node) throws ParserConfigurationException, TransformerException {
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    StringWriter writer = new StringWriter();
    DOMSource source = new DOMSource(node);
    StreamResult result = new StreamResult(writer);
    transformer.transform(source, result);
    return writer.toString();
}

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.
 *///from   w  w w.  j a  va2  s . c o m
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

/**
 * Convert a DOM tree into a String using transform
 * @param domDoc                  DOM object
 * @throws java.io.IOException    I/O exception
 * @return                        XML as String
 *///ww w  . j ava  2  s. com
public static String docToString2(Document domDoc) throws IOException {
    try {
        TransformerFactory transFact = TransformerFactory.newInstance();
        Transformer trans = transFact.newTransformer();
        trans.setOutputProperty(OutputKeys.INDENT, "no");
        StringWriter sw = new StringWriter();
        Result result = new StreamResult(sw);
        trans.transform(new DOMSource(domDoc), result);
        return sw.toString();
    } catch (Exception ex) {
        throw new IOException(String.format("Error converting from doc to string %s", ex.getMessage()));
    }
}

From source file:Main.java

/**
 * Filters an XML document.//  w  w  w .j  av  a  2 s.  c  o m
 * 
 * @param source the input source for the XML document
 * @param filter the filter
 * 
 * @return an input source for the resulting document
 * 
 * @throws Exception
 */
public static InputSource filterXml(InputSource source, XMLFilter filter) throws Exception {
    // Create filter, which uses a "real" XMLReader but also removes the
    // attributes
    XMLReader reader = XMLReaderFactory.createXMLReader();
    filter.setParent(reader);

    // Create a Transformer
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();

    // Transform the source tree, applying the filter, and store the result
    // tree in a character array
    CharArrayWriter writer = new CharArrayWriter();
    t.transform(new SAXSource(filter, source), new StreamResult(writer));

    // Return a new InputSource using the result tree
    return new InputSource(new CharArrayReader(writer.toCharArray()));
}

From source file:Main.java

public static String Doc2String(Document paramDocument) {
    try {/*from   ww  w  . ja  v a 2s. c o  m*/
        DOMSource localDOMSource = new DOMSource(paramDocument);
        StringWriter localStringWriter = new StringWriter();
        StreamResult localStreamResult = new StreamResult(localStringWriter);
        TransformerFactory localTransformerFactory = TransformerFactory.newInstance();
        Transformer localTransformer = localTransformerFactory.newTransformer();
        localTransformer.transform(localDOMSource, localStreamResult);
        return localStringWriter.toString();
    } catch (Exception localException) {
        localException.printStackTrace();
    }
    return null;
}

From source file:Main.java

/** Write a DOM document to a string */
public static String xmlDocumentToString(Document document) {
    try {/*from   www .ja v  a  2s.c  o  m*/
        Source source = new DOMSource(document);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static String getXmlString(Document document, String tagName)
        throws ParserConfigurationException, SAXException, IOException, TransformerException {
    NodeList nodeList = document.getElementsByTagName(tagName);
    if (nodeList.getLength() < 1) {
        throw new IllegalArgumentException("no " + tagName + " found");
    }//  www.  j  ava  2s .  c  o  m
    Node sub = nodeList.item(0);
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document subdoc = db.newDocument();
    subdoc.appendChild(sub.cloneNode(true));

    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    DOMSource domSource = new DOMSource(subdoc);
    StringWriter sw = new StringWriter();
    StreamResult xmlResult = new StreamResult(sw);
    transformer.transform(domSource, xmlResult);
    sw.flush();
    return sw.toString();
}