Java XML Node Save xmltostring(String indent, Node node)

Here you can find the source of xmltostring(String indent, Node node)

Description

xmltostring

License

Apache License

Declaration

public static String xmltostring(String indent, Node node) 

Method Source Code

//package com.java2s;
/*/*w ww.  j av a 2s.  c o  m*/
 * Copyright 2006-2011 The Virtual Laboratory for e-Science (VL-e) 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License").  
 * You may not use this file except in compliance with the License. 
 * For details, see the LICENCE.txt file location in the root directory of this 
 * distribution or obtain the Apache Licence at the following location: 
 *     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.
 * 
 * See: http://www.vl-e.nl/ 
 * See: LICENCE.txt (located in the root folder of this distribution). 
 * ---
 * $Id: WMSUtil.java,v 1.15 2011-04-18 12:28:51 ptdeboer Exp $  
 * $Date: 2011-04-18 12:28:51 $
 */

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static String xmltostring(String indent, Node node) {
        if (indent == null)
            indent = "";

        String str = "";

        short type = node.getNodeType();
        str += indent + " <" + node.getNodeName() + "(#" + type + ")" + "='" + node.getNodeValue() + "'>\n";

        NamedNodeMap attrs = node.getAttributes();

        if (attrs != null)
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = attrs.item(i);
                type = attr.getNodeType();
                str += indent + "  + " + attr.getNodeName() + "(#" + type + ")" + "='" + attr.getNodeValue()
                        + "'\n";
            }

        if (node instanceof Element) {
            Element el = (Element) node;

            NodeList childs = el.getChildNodes();
            for (int i = 0; i < childs.getLength(); i++) {
                str += xmltostring(indent + "   ", childs.item(i));
            }
        }

        return str;
    }
}

Related

  1. writeXml(final Node node)
  2. writeXMLwalkTree(Node node, int indent, PrintWriter out)
  3. writeXMLwithXALAN(final Writer writer, final Node node, final String xmlEncoding)
  4. writeXMLwithXALAN(final Writer writer, final Node node, final String xmlEncoding)
  5. xmlSelectNodes0(List list, Node node, String path[], int n)