Here you can find the source of serialize(Element e, boolean omitXMLDeclaration)
Parameter | Description |
---|---|
e | Element to be serialized. |
omitXMLDeclaration | boolean representing whether or not to omit the XML declaration. |
public static final String serialize(Element e, boolean omitXMLDeclaration)
//package com.java2s; /******************************************************************************* * Copyright (c) 2004, 2005 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/* ww w .j av a2 s. c o m*/ * IBM Corporation - initial API and implementation *******************************************************************************/ import java.io.ByteArrayOutputStream; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.DocumentFragment; import org.w3c.dom.Element; public class Main { public static final String UTF8_ENCODING = "UTF-8"; /** * Serialize an XML Element into a String. * @param e Element to be serialized. * @param omitXMLDeclaration boolean representing whether or not to omit the XML declaration. * @return String representation of the XML document fragment. */ public static final String serialize(Element e, boolean omitXMLDeclaration) { if (e != null) { try { DOMSource domSource = new DOMSource(e); Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, ((omitXMLDeclaration) ? "yes" : "no")); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.ENCODING, UTF8_ENCODING); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); serializer.transform(domSource, new StreamResult(baos)); baos.close(); return new String(baos.toByteArray(), UTF8_ENCODING); } catch (Throwable t) { } } return null; } /** * Serialize an XML Element into a String. * @param df DocumentFragment to be serialized. * @param omitXMLDeclaration boolean representing whether or not to omit the XML declaration. * @return String representation of the XML document fragment. */ public static final String serialize(DocumentFragment df, boolean omitXMLDeclaration) { if (df != null) { try { DOMSource domSource = new DOMSource(df); Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, ((omitXMLDeclaration) ? "yes" : "no")); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.ENCODING, UTF8_ENCODING); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); serializer.transform(domSource, new StreamResult(baos)); baos.close(); return new String(baos.toByteArray(), UTF8_ENCODING); } catch (Throwable t) { } } return null; } }