Example usage for javax.xml.transform TransformerException printStackTrace

List of usage examples for javax.xml.transform TransformerException printStackTrace

Introduction

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

Prototype

@Override
public void printStackTrace() 

Source Link

Document

Print the the trace of methods from where the error originated.

Usage

From source file:Main.java

public static String documentToString(Document document) {
        try {/*from w w  w  . j ava  2s.  co  m*/
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer trans = tf.newTransformer();
            StringWriter sw = new StringWriter();
            trans.transform(new DOMSource(document), new StreamResult(sw));
            return sw.toString();
        } catch (TransformerException tEx) {
            tEx.printStackTrace();
        }
        return null;
    }

From source file:Main.java

static public Document cloneDocument(Document document) {
    if (document == null)
        return null;
    Document result = newDocument();
    try {//www .j  a va2 s. c om
        identityTransformer.transform(new DOMSource(document), new DOMResult(result));
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static String xmlDocumentToString(final Document doc) {
    try {/*from   w w  w. j av  a 2  s .  c  o  m*/
        final DOMSource domSource = new DOMSource(doc);
        final StringWriter writer = new StringWriter();
        final StreamResult result = new StreamResult(writer);
        final TransformerFactory tf = TransformerFactory.newInstance();
        final Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (final TransformerException ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static void saveDoc(Document doc, String fileName) {
    try {// w ww  . j a 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 saveXml(Document doc, String filename) {
    try {/* w  w  w.  ja v  a2  s.c o m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        DOMSource source = new DOMSource(doc);
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
        StreamResult result = new StreamResult(pw);
        transformer.transform(source, result);
    } catch (TransformerException tfe) {
        tfe.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void printNode(Node node) {
    try {// www . j a  v  a  2s  .c o  m
        // Set up the output transformer
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");

        // Print the DOM node

        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(node);
        trans.transform(source, result);
        String xmlString = sw.toString();

        System.out.println(xmlString);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String xmlToString(Document doc) {
    try {/*from w  w  w .  j ava 2  s  . com*/
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (TransformerException ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String sourceToXMLString(Source result) {

    String xmlResult = null;/*from w ww  .j  av a  2  s. com*/
    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;
}

From source file:Main.java

private static boolean writeTo(Document doc, String fileName) throws Exception {
    boolean isOver = false;
    DOMSource doms = new DOMSource(doc);
    File f = new File(fileName);
    StreamResult sr = new StreamResult(f);
    try {/*  w w w . ja v  a 2  s . com*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        Properties properties = t.getOutputProperties();
        properties.setProperty(OutputKeys.ENCODING, "UTF-8");
        t.setOutputProperties(properties);
        t.transform(doms, sr);
        isOver = true;
    } catch (TransformerConfigurationException tce) {
        tce.printStackTrace();
    } catch (TransformerException te) {
        te.printStackTrace();
    }
    return isOver;
}

From source file:Main.java

public static void saveReportToFile(Node node, String documentFileName) {
    // StringWriter writer = new StringWriter();
    // StreamResult resultString = new StreamResult(writer);

    File output = new File(documentFileName);
    StreamResult resultFile = new StreamResult(output);

    DOMSource source = new DOMSource(node);
    try {//from   w  w  w .ja  v a  2s  .  c om
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        t.transform(source, resultFile);
        // t.transform(source, resultString);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    // return writer.toString();
}