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:org.opencastproject.remotetest.util.Utils.java

/**
 * Converts the node to a string representation.
 * // ww w. ja va 2 s.c o m
 * @param node
 *          the node
 * @return the string representation
 * @throws Exception
 */
public static String nodeToString(Node node) throws Exception {
    DOMSource domSource = new DOMSource(node);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(out);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.transform(domSource, result);
    InputStream in = new ByteArrayInputStream(out.toByteArray());
    return IOUtils.toString(in, "UTF-8");
}

From source file:Main.java

public static String toString(Document doc) {
    try {/*from w  w w.j a v a2 s.co m*/
        StringWriter sw = new StringWriter();
        Source source = new DOMSource(doc);
        Result result = new StreamResult(sw);

        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", new Integer(4));
        Transformer xformer = tf.newTransformer();
        xformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.transform(source, result);
        return sw.toString();
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

private static void writeTo(Result result, Document document) throws TransformerConfigurationException,
        TransformerException, FileNotFoundException, UnsupportedEncodingException {

    // Use a Transformer for output
    TransformerFactory tFactory = TransformerFactory.newInstance();
    tFactory.setAttribute("indent-number", new Integer(4));

    Transformer transformer = tFactory.newTransformer();
    DOMSource source = new DOMSource(document);

    transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // NOI18N
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N
    transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); // NOI18N
    transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); // NOI18N

    // indent the output to make it more legible... 
    try {//from w  w w.j  a  v a 2  s. com
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // NOI18N
        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N
    } catch (IllegalArgumentException e) {
        // the JAXP implementation doesn't support indentation, no big deal
        //e.printStackTrace();
    }

    transformer.transform(source, result);
}

From source file:com.valco.utility.FacturasUtility.java

public static OutputStream getCadenaOriginal(String cadenaOriginalDir, String xml) throws Exception {
    StreamSource sourceXSL = new StreamSource(new File(cadenaOriginalDir));
    StringReader reader = new StringReader(xml);
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    transformer = tFactory.newTransformer(sourceXSL);
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    OutputStream output = new ByteArrayOutputStream();
    transformer.transform(new StreamSource(reader), new StreamResult(output));

    return output;
}

From source file:com.marklogic.mapreduce.DOMDocument.java

static ByteArrayOutputStream serialize(Node node) throws TransformerException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Result rslt = new StreamResult(bos);
    Source src = new DOMSource(node);
    Transformer transformer = getTransformerFactory().newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(src, rslt);//  w  w w  .  ja  v a  2 s  .  c o m

    return bos;
}

From source file:Main.java

private static boolean updateXML(Document document, String path) throws TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();

    // out put encoding.
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

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

    DocumentType type = document.getDoctype();
    if (type != null) {
        System.out.println("doctype -->" + type.getPublicId());
        transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, type.getPublicId());
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, type.getSystemId());
    }//from   www  .j  a  va  2 s .c  o  m

    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(new File(path));
    transformer.transform(source, result);

    transformer.reset();
    return true;
}

From source file:XMLUtil.java

public static void write(Document doc, OutputStream out) throws IOException {
    // XXX note that this may fail to write out namespaces correctly if the
    // document//from   w  w w .j av  a 2 s. co  m
    // is created with namespaces and no explicit prefixes; however no code in
    // this package is likely to be doing so
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        DocumentType dt = doc.getDoctype();
        if (dt != null) {
            String pub = dt.getPublicId();
            if (pub != null) {
                t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pub);
            }
            t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dt.getSystemId());
        }
        t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N
        t.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // NOI18N
        Source source = new DOMSource(doc);
        Result result = new StreamResult(out);
        t.transform(source, result);
    } catch (Exception e) {
        throw (IOException) new IOException(e.toString()).initCause(e);
    } catch (TransformerFactoryConfigurationError e) {
        throw (IOException) new IOException(e.toString()).initCause(e);
    }
}

From source file:com.hangum.tadpole.engine.sql.util.export.XMLExporter.java

/**
 * make content//w  w w .  ja v  a 2s  .  co m
 * 
 * @param tableName
 * @param rsDAO
 * @return
 */
public static String makeContent(String tableName, QueryExecuteResultDTO rsDAO, int intLimitCnt)
        throws Exception {
    final StringWriter stWriter = new StringWriter();
    final List<Map<Integer, Object>> dataList = rsDAO.getDataList().getData();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    final Document doc = builder.newDocument();
    final Element results = doc.createElement(tableName);
    doc.appendChild(results);

    Map<Integer, String> mapLabelName = rsDAO.getColumnLabelName();
    for (int i = 0; i < dataList.size(); i++) {
        Map<Integer, Object> mapColumns = dataList.get(i);
        Element row = doc.createElement("Row");
        results.appendChild(row);

        for (int j = 1; j < mapColumns.size(); j++) {
            String columnName = mapLabelName.get(j);
            String strValue = mapColumns.get(j) == null ? "" : "" + mapColumns.get(j);

            Element node = doc.createElement(columnName);
            node.appendChild(doc.createTextNode(strValue));
            row.appendChild(node);
        }

        if (i == intLimitCnt)
            break;
    }

    DOMSource domSource = new DOMSource(doc);
    TransformerFactory tf = TransformerFactory.newInstance();
    tf.setAttribute("indent-number", 4);

    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");//"ISO-8859-1");
    StreamResult sr = new StreamResult(stWriter);
    transformer.transform(domSource, sr);

    return stWriter.toString();
}

From source file:Main.java

public static final String indenting(Node rootNode) throws XPathExpressionException, TransformerException {

    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", rootNode,
            XPathConstants.NODESET);

    for (int i = 0; i < nodeList.getLength(); ++i) {
        Node node = nodeList.item(i);
        node.getParentNode().removeChild(node);
    }//from  w  w  w  .j a  va2 s.  co m

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

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

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

    return stringWriter.toString();
}

From source file:Main.java

/**
 * DOCUMENT ME!//from w  w  w . j a  v  a2  s.  c om
 *
 * @return DOCUMENT ME!
 *
 * @throws TransformerConfigurationException DOCUMENT ME!
 * @throws TransformerException DOCUMENT ME!
 * @throws Exception DOCUMENT ME!
 */
public static byte[] writeToBytes(Document document)
        throws TransformerConfigurationException, TransformerException, Exception {
    byte[] ret = null;

    TransformerFactory tFactory = TransformerFactory.newInstance();
    tFactory.setAttribute("indent-number", new Integer(4));

    Transformer transformer = tFactory.newTransformer();
    DOMSource source = new DOMSource(document);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(bos);
    transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // NOI18N
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N
    transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); // NOI18N
    transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); // NOI18N

    // indent the output to make it more legible...
    try {
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // NOI18N
        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N
    } catch (IllegalArgumentException e) {
        // the JAXP implementation doesn't support indentation, no big deal
        //e.printStackTrace();
    }

    try {
        transformer.transform(source, result);
        ret = bos.toByteArray();
    } finally {
        if (bos != null) {
            bos.flush();
            bos.close();
        }
    }

    return ret;
}