Java XML Document Format formatXML(Document document, org.w3c.dom.Element root, String tab)

Here you can find the source of formatXML(Document document, org.w3c.dom.Element root, String tab)

Description

format XML into human readable form

License

Open Source License

Parameter

Parameter Description
document a parameter
root a parameter
tab a parameter

Declaration

private static void formatXML(Document document, org.w3c.dom.Element root, String tab) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.Document;

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

public class Main {
    /**/*  www  .j  a  v a  2 s  .c  o m*/
     * format XML into human readable form
     * @param document
     * @param root
     * @param tab
     */
    private static void formatXML(Document document, org.w3c.dom.Element root, String tab) {
        NodeList children = root.getChildNodes();
        // save the nodes in the array first
        Node[] nodes = new Node[children.getLength()];
        for (int i = 0; i < children.getLength(); i++)
            nodes[i] = children.item(i);
        // insert identations
        for (int i = 0; i < nodes.length; i++) {
            root.insertBefore(document.createTextNode("\n" + tab), nodes[i]);
            if (nodes[i] instanceof org.w3c.dom.Element)
                formatXML(document, (org.w3c.dom.Element) nodes[i], "  " + tab);
        }
        root.appendChild(document.createTextNode("\n" + tab.substring(0, tab.length() - 2)));
    }
}

Related

  1. format(Source xslSource, Document xml, URIResolver resolver)
  2. formatElementEnd( Document document, Element element, String formatString)
  3. formatElementStart( Document document, Element element, String formatString)
  4. formatNode(Document doc, Node parent, Node node)
  5. formatPython(int start, Document doc, String s)
  6. formatXML(Document xml, Document xsl)
  7. formatXML(Document xml, Document xsl, URIResolver resolver, Writer output)
  8. pretty(Document document, OutputStream outputStream, int indent)
  9. prettyFormat(Document doc)