Example usage for org.w3c.dom Document getInputEncoding

List of usage examples for org.w3c.dom Document getInputEncoding

Introduction

In this page you can find the example usage for org.w3c.dom Document getInputEncoding.

Prototype

public String getInputEncoding();

Source Link

Document

An attribute specifying the encoding used for this document at the time of the parsing.

Usage

From source file:Main.java

License:asdf

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder(); // Create the parser
    Document xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));

    System.out.println(xmlDoc.getInputEncoding());

}

From source file:Main.java

public static String serializeToString(Document doc) throws IOException {
    String encoding = doc.getInputEncoding();
    if (encoding == null)
        encoding = "UTF-8";

    return serializeToString(doc, encoding);
}

From source file:Main.java

/**
 * Serialize XML Document to string using DOM Level 3 Load/Save
 * /*from w w w .ja  v a 2  s  .c om*/
 * @param doc XML Document
 * @param node the Node to serialize
 * @return String representation of the Document
 * @throws IOException
 */
public static String serializeToStringLS(Document doc, Node node) throws IOException {
    String encoding = doc.getInputEncoding();
    if (encoding == null)
        encoding = "UTF-8";

    return serializeToStringLS(doc, node, encoding);
}

From source file:Main.java

public static byte[] serializeToByteArray(Document doc) throws IOException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {//from  w  w w .j  av a2 s.  c o  m
        transformer = tFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new IOException("Unable to serialize XML document");
    }
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    String encoding = doc.getInputEncoding();
    if (encoding == null)
        encoding = "UTF-8";
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    DOMSource source = new DOMSource(doc);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Result result = new StreamResult(out);
    try {
        transformer.transform(source, result);
    } catch (TransformerException e) {
        throw new IOException("Unable to serialize XML document");
    }

    return out.toByteArray();
}

From source file:com.autentia.mvn.plugin.changes.BugzillaChangesMojo.java

/**
 * Saves new changes document/*from  w  w w  .j a  va  2s. c o  m*/
 * 
 * @param changesDocument
 * @throws IOException
 */
private void saveChangesDocument(final Document changesDocument) throws IOException {
    final OutputFormat outputFormat = new OutputFormat();
    outputFormat.setEncoding(changesDocument.getInputEncoding());
    outputFormat.setIndenting(true);
    outputFormat.setMethod("XML");
    final FileOutputStream fos = new FileOutputStream(this.xmlPath);
    final XMLSerializer serializer = new XMLSerializer(fos, outputFormat);
    serializer.serialize(changesDocument);
    fos.flush();
    fos.close();
}

From source file:org.hippoecm.frontend.plugins.gallery.imageutil.ScaleImageOperation.java

private void writeSvgDocument(final File file, final Document svgDocument) {
    try {//w ww .  j  av a 2 s.  c  o m
        final Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, svgDocument.getInputEncoding());
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(2));
        Result output = new StreamResult(file);
        Source input = new DOMSource(svgDocument);
        transformer.transform(input, output);
    } catch (TransformerConfigurationException e) {
        log.info("Writing SVG file " + file.getName() + " failed, using original instead", e);
    } catch (TransformerException e) {
        log.info("Writing SVG file " + file.getName() + " failed, using original instead", e);
    }
}

From source file:org.kalypso.model.hydrology.util.optimize.NAOptimizingJob.java

private void saveOptimizeConfig(final File file) {
    try {//from   ww w .ja va  2  s . c  om
        final TransformerFactory factory = TransformerFactory.newInstance();
        final Transformer t = factory.newTransformer();

        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); //$NON-NLS-1$ //$NON-NLS-2$
        t.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$

        final Node naOptimizeDom = m_data.getOptimizeData().getOptimizeDom();
        final Document ownerDocument = naOptimizeDom instanceof Document ? (Document) naOptimizeDom
                : naOptimizeDom.getOwnerDocument();
        final String encoding = ownerDocument.getInputEncoding();
        t.setOutputProperty(OutputKeys.ENCODING, encoding);
        t.transform(new DOMSource(ownerDocument), new StreamResult(file));
    } catch (final Exception e) {
        e.printStackTrace();
    }
}