Java XML Node Save writeXml(final Node node)

Here you can find the source of writeXml(final Node node)

Description

Escribe un XML como texto.

License

Open Source License

Parameter

Parameter Description
node Nodo XML que queremos pasar a texto.

Return

Cadena de texto con el XML en forma de array de octetos

Declaration

private static byte[] writeXml(final Node node) 

Method Source Code

//package com.java2s;
/* Copyright (C) 2011 [Gobierno de Espana]
 * This file is part of "Cliente @Firma".
 * "Cliente @Firma" is free software; you can redistribute it and/or modify it under the terms of:
 *   - the GNU General Public License as published by the Free Software Foundation;
 *     either version 2 of the License, or (at your option) any later version.
 *   - or The European Software License; either version 1.1 or (at your option) any later version.
 * Date: 11/01/11// w  w  w . jav a  2s . c  o  m
 * You may contact the copyright holder at: soporte.afirma5@mpt.es
 */

import java.io.ByteArrayOutputStream;

import org.w3c.dom.DOMConfiguration;

import org.w3c.dom.Node;

import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;

public class Main {
    private static final String DEFAULT_ENCODING = "UTF-8";

    /** Escribe un XML como texto.
     * @param node Nodo XML que queremos pasar a texto.
     * @return Cadena de texto con el XML en forma de array de octetos */
    private static byte[] writeXml(final Node node) {
        final DOMImplementationLS domImpl = (DOMImplementationLS) node.getOwnerDocument().getImplementation();
        final LSSerializer lsSerializer = domImpl.createLSSerializer();
        final DOMConfiguration domConfiguration = lsSerializer.getDomConfig();
        if (domConfiguration.canSetParameter("namespaces", Boolean.FALSE)) { //$NON-NLS-1$
            domConfiguration.setParameter("namespaces", Boolean.FALSE); //$NON-NLS-1$
        }
        if (domConfiguration.canSetParameter("canonical-form", Boolean.TRUE)) { //$NON-NLS-1$
            lsSerializer.getDomConfig().setParameter("canonical-form", Boolean.TRUE); //$NON-NLS-1$
        }
        final LSOutput lsOutput = domImpl.createLSOutput();
        lsOutput.setEncoding(DEFAULT_ENCODING);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        lsOutput.setByteStream(baos);
        lsSerializer.write(node, lsOutput);
        return baos.toByteArray();
    }
}

Related

  1. writeNode(Node node, OutputStream output, Map serializerParams)
  2. writeNode(PrintWriter w, Node node, int depth)
  3. writeToStream(OutputStream os, Node node)
  4. writeToString(Node node)
  5. writeToString(Node node)
  6. writeXMLwalkTree(Node node, int indent, PrintWriter out)
  7. writeXMLwithXALAN(final Writer writer, final Node node, final String xmlEncoding)
  8. writeXMLwithXALAN(final Writer writer, final Node node, final String xmlEncoding)
  9. xmlSelectNodes0(List list, Node node, String path[], int n)