Here you can find the source of writeXml(final Node node)
Parameter | Description |
---|---|
node | Nodo XML que queremos pasar a texto. |
private static byte[] writeXml(final Node node)
//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(); } }