Example usage for javax.xml.transform OutputKeys ENCODING

List of usage examples for javax.xml.transform OutputKeys ENCODING

Introduction

In this page you can find the example usage for javax.xml.transform OutputKeys ENCODING.

Prototype

String ENCODING

To view the source code for javax.xml.transform OutputKeys ENCODING.

Click Source Link

Document

encoding = string.

Usage

From source file:Main.java

private static String toString(final Document document, final boolean indent, final boolean omitXmlDeclaration)
        throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException {
    final Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
    transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    final StringWriter stringWriter = new StringWriter();
    final StreamResult streamResult = new StreamResult(stringWriter);

    transformer.transform(new DOMSource(document), streamResult);

    return stringWriter.toString();
}

From source file:Main.java

static public void toHtml(Document xmldoc, OutputStream os) throws TransformerException {
    StreamResult out = new StreamResult(os);
    DOMSource domSource = new DOMSource(xmldoc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "html");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(domSource, out);
}

From source file:Main.java

public static void format(Source source, boolean omitDeclaration, Result result) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    try {//w  ww.  j a v a 2 s . c om
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    } catch (IllegalArgumentException exc) {
        // not supported
    }

    if (omitDeclaration) {
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    }
    transformer.transform(source, result);
}

From source file:Main.java

public static boolean ExportXML(Document xmlOutput, String filename) {
    try {//from  w  w  w .  j a v a  2  s  . c o  m
        if (xmlOutput != null) {
            Source source = new DOMSource(xmlOutput);
            FileOutputStream file = new FileOutputStream(filename);
            StreamResult res = new StreamResult(file);
            Transformer xformer = TransformerFactory.newInstance().newTransformer();
            xformer.setOutputProperty(OutputKeys.ENCODING, "ISO8859-1");
            xformer.setOutputProperty(OutputKeys.INDENT, "yes");

            xformer.transform(source, res);
            file.close();

            return true;
        }
        return false;
    } catch (FileNotFoundException e) {
        return false;
    } catch (TransformerConfigurationException e) {
        return false;
    } catch (TransformerException e) {
        return false;
    } catch (IOException e) {
        return false;
    }
}

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 {//from   ww  w . j a  v  a  2  s  .  co  m
        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 printDocument(Document doc, OutputStream out) 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.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

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

From source file:Main.java

public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty("omit-xml-declaration", "no");
    transformer.setOutputProperty("method", "xml");
    transformer.setOutputProperty("indent", "yes");
    transformer.setOutputProperty("encoding", "UTF-8");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

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

From source file:Main.java

/**
 * Transform xml Document to String/*  w  w  w. jav  a  2s.c  o  m*/
 * @param doc Xml Document
 * @return String representation
 */
public static String getContent(Document doc) {
    try {
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        transformer.transform(new DOMSource(doc), new StreamResult(sw));
        return sw.toString();
    } catch (Exception ex) {
        throw new RuntimeException("Error converting to String", ex);
    }
}

From source file:Main.java

public static void save(Document document, IFile file, IProgressMonitor monitor)
        throws TransformerException, CoreException, UnsupportedEncodingException {
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.ENCODING, "utf-8");
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    trans.setOutputProperty(OutputKeys.INDENT, "yes");

    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    DOMSource ds = new DOMSource(document);
    trans.transform(ds, sr);//  w  w w .ja  va 2s  .  c  o  m
    String xmlString = sw.toString();

    InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));

    if (file.exists()) {
        file.setContents(is, 0, monitor);
    } else {
        file.create(is, true, monitor);
    }
}

From source file:Main.java

public static void printDocument(Document doc) {
    try {/*from   ww  w.  ja  v a 2s.  com*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(new DOMSource(doc),
                new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}