Java XML Node Serialize serializeXML(Node root)

Here you can find the source of serializeXML(Node root)

Description

This method serializes the supplied XML.

License

Apache License

Declaration

public static String serializeXML(Node root) 

Method Source Code

//package com.java2s;
/**//from w  w w. j a va2  s .  c o  m
 * Copyright 2014 IBM Corp.
 *
 * Licensed 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.*;
import org.w3c.dom.ls.*;

public class Main {
    /**
     * This method serializes the supplied XML.  It returns something like this:
     * 
     * <?xml version="1.0" encoding="UTF-16"?>
     * <soma:response xmlns:soma="http://www.datapower.com/schemas/management">
     *     <soma:timestamp>2008-11-26T19:18:01-05:00</soma:timestamp>
     *     <soma:result>OK</soma:result>
     * </soma:response>
     * 
     * Yes, it encodes with UTF-16 rather than the preferred UTF-8.  Someone ought to
     * invest the time to correct this behavior.
     */
    public static String serializeXML(Node root) {
        if (root == null)
            return "<null/>";
        if (root.getFeature("Core", "3.0") == null)
            throw new UnsupportedOperationException(
                    "JVM doesn't support DOM Core 3.0");
        if (root.getFeature("LS", "3.0") == null)
            throw new UnsupportedOperationException(
                    "JVM doesn't support DOM LS 3.0");

        DOMImplementationLS ls = (DOMImplementationLS) (root
                .getOwnerDocument().getImplementation()).getFeature("LS",
                "3.0");
        LSSerializer lss = ls.createLSSerializer();
        String result = lss.writeToString(root);

        return result;
    }
}

Related

  1. serializeToXML(Node node, boolean indent)
  2. serializeToXml(Node node, OutputStream out)
  3. serializeXML(Node e, Writer out)
  4. serializeXML(Node node)
  5. serializeXML(Node node)
  6. serializeXmlNode(Node node, Writer writer, boolean includeNode)