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

/**
 * Get standard xml string of node//  w ww.  ja v  a  2 s .c o m
 * 
 * @param node
 * @return
 */
public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        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 e) {
        e.printStackTrace();
    }
    return sw.toString();
}

From source file:Main.java

public static String getXMLString(Document dom, boolean bOmitDeclaration, String sEncoding) {
    String sOmit = (bOmitDeclaration ? "yes" : "no");
    try {/*  w  w w.  ja  v  a2 s. co  m*/
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        if (sEncoding != null)
            transformer.setOutputProperty(OutputKeys.ENCODING, sEncoding);
        StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, sOmit);
        transformer.transform(new DOMSource(dom), new StreamResult(buffer));
        return buffer.toString();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * src:http://www.journaldev.com/1237/java-convert-string-to-xml-document-and-xml-document-to-string
 * @param doc/*from   w  w  w .  j av a2  s  .  com*/
 * @return
 */
public static String convertDocumentToString(Document doc) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;
    try {
        transformer = tf.newTransformer();
        // below code to remove XML declaration
        // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        return output;
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

/**
 * Method to get formatted Xml string from Xml source
 *
 * @param payload Source//from  w ww.  j a va  2  s.  co m
 * @return a formatted Xml string
 */
public static String getXmlStringFromSource(Source payload) {
    String result = null;
    StreamResult strResult = new StreamResult(new StringWriter());

    if (payload != null) {
        try {
            TransformerFactory factory = TransformerFactory.newInstance();
            //factory.setAttribute("indent-number", new Integer(2));
            Transformer transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "1");
            transformer.transform(payload, strResult);
        } catch (TransformerException e) {
            e.printStackTrace();
        }
        result = strResult.getWriter().toString();
    }
    return result;
}

From source file:Main.java

/**
 * method used to convert a xml document to a string
 * /*  w w w  .  j av  a  2 s . c om*/
 * @param doc
 * @return
 */
public static String convertDocumentToString(Document doc) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;

    try {
        /**
         * transformation of document happens here
         */
        transformer = tf.newTransformer();
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        String output = writer.getBuffer().toString();

        /**
         * logging success
         */
        logger.info("xml conversion to string  successful Class:XMLParserUtility line# 98");

        return output;

    } catch (TransformerException e) {
        e.printStackTrace();
        logger.fatal("Could not transform document to XML", e);
    }
    return null;
}

From source file:Main.java

private static void write(Document doc) {
    try {/*w w  w . j ava  2s  . co m*/
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = tFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("hooks.xml"));
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
        transformer.transform(source, result);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void save(String paramString, Document paramDocument) throws Exception {
    DOMSource localDOMSource = new DOMSource(paramDocument);
    File localFile1 = new File(paramString);
    File localFile2 = localFile1.getParentFile();
    localFile2.mkdirs();//  w  w w.j  av  a  2 s  .c  o m
    StreamResult localStreamResult = new StreamResult(localFile1);
    try {
        TransformerFactory localTransformerFactory = TransformerFactory.newInstance();
        Transformer localTransformer = localTransformerFactory.newTransformer();
        Properties localProperties = localTransformer.getOutputProperties();
        localProperties.setProperty("encoding", "UTF-8");
        localProperties.setProperty("indent", "yes");
        localTransformer.setOutputProperties(localProperties);
        localTransformer.transform(localDOMSource, localStreamResult);
    } catch (TransformerConfigurationException localTransformerConfigurationException) {
        localTransformerConfigurationException.printStackTrace();
    } catch (TransformerException localTransformerException) {
        localTransformerException.printStackTrace();
    }
}

From source file:Main.java

public static String writeDocumentToString(Document document) {
    try {//from  ww w  . ja  va2  s . c  o  m
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans;
        trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        StringWriter writer = new StringWriter();
        try {
            StreamResult streamResult = new StreamResult(writer);
            DOMSource source = new DOMSource(document);
            trans.transform(source, streamResult);
        } catch (TransformerException e) {
            e.printStackTrace();
            return null;
        } finally {
            writer.close();
        }
        return writer.toString();

    } catch (TransformerConfigurationException | IOException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

public static String documentToString(Document doc) {
    StreamResult sr = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    try {//w ww  . j  a v  a  2  s.  c  o  m
        _transformer.transform(source, sr);
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return sr.getWriter().toString();
}

From source file:Main.java

public static void documentToFile(Document doc, File file, Charset charset) throws IOException {
    StreamResult sr = new StreamResult(new OutputStreamWriter(new FileOutputStream(file), charset));
    DOMSource source = new DOMSource(doc);
    try {//from  w w w  . j  av  a 2s  .  com
        _transformer.transform(source, sr);
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}