Java XML Node Serialize serializeToXML(Node node, boolean indent)

Here you can find the source of serializeToXML(Node node, boolean indent)

Description

Given a DOM Node produces the XML serialization omitting the XML declaration.

License

Apache License

Parameter

Parameter Description
node node to be serialized.
indent if <code>true</code> the output is indented.

Exception

Parameter Description
TransformerException if an error occurs during theserializator initialization and activation.

Return

the XML serialization.

Declaration

public static String serializeToXML(Node node, boolean indent) throws TransformerException, IOException 

Method Source Code

//package com.java2s;
/*//from w ww .j av  a2  s  .c o m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.w3c.dom.Node;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import java.io.IOException;
import java.io.StringWriter;

public class Main {
    /**
     * Given a <i>DOM</i> {@link Node} produces the <i>XML</i> serialization
     * omitting the <i>XML declaration</i>.
     *
     * @param node node to be serialized.
     * @param indent if <code>true</code> the output is indented.
     * @return the XML serialization.
     * @throws TransformerException if an error occurs during the
     *         serializator initialization and activation.
     * @throws java.io.IOException
     */
    public static String serializeToXML(Node node, boolean indent) throws TransformerException, IOException {
        final DOMSource domSource = new DOMSource(node);
        final Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        if (indent) {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        }
        final StringWriter sw = new StringWriter();
        final StreamResult sr = new StreamResult(sw);
        transformer.transform(domSource, sr);
        sw.close();
        return sw.toString();
    }
}

Related

  1. serializeNode(Node xmlNode)
  2. serializeNoNS(Node node)
  3. serializeRDFToHTML(String xmlRDF)
  4. serializeSource(Source source)
  5. serializeToString(Node node, String encoding)
  6. serializeToXml(Node node, OutputStream out)
  7. serializeXML(Node e, Writer out)
  8. serializeXML(Node node)
  9. serializeXML(Node node)