Java XML Document to String toStructureString(Document document)

Here you can find the source of toStructureString(Document document)

Description

to Structure String

License

Apache License

Declaration

public static String toStructureString(Document document) 

Method Source Code


//package com.java2s;
/*/*from w ww .j a v  a2  s  . co m*/
 * Copyright 2004-2007 the Seasar Foundation and the Others.
 *
 * 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 java.io.PrintWriter;
import java.io.StringWriter;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static String toStructureString(Document document) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        NodeList childNodes = document.getChildNodes();
        printNodes(pw, childNodes, 0);
        return sw.toString();
    }

    private static void printNodes(PrintWriter pw, NodeList childNodes, int indent) {
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node node = childNodes.item(i);
            printIndent(pw, indent);
            String nodeName = node.getNodeName();
            if (Node.TEXT_NODE == node.getNodeType()) {
                if (node.hasChildNodes()) {
                    throw new RuntimeException();
                } else {
                    pw.println("[" + nodeName + "]");
                }
            } else {
                if (node.hasChildNodes()) {
                    pw.println("<" + nodeName + ">");
                    printNodes(pw, node.getChildNodes(), indent + 1);
                    printIndent(pw, indent);
                    pw.println("</" + nodeName + ">");
                } else {
                    pw.println("<" + nodeName + " />");
                }
            }
        }
    }

    private static void printIndent(PrintWriter pw, int indent) {
        for (int i = 0; i < indent; i++) {
            pw.print("  ");
        }
    }
}

Related

  1. toString(final Document document)
  2. toString(final Document document)
  3. toString(final Document document, final boolean indent, final boolean omitXmlDeclaration)
  4. toString(Node document)
  5. toStringFromDoc(Document document)
  6. toXML(Document document)
  7. toXML(Document dom)
  8. toXml(Document domDoc)
  9. toXmlString(Document doc)