Java XML Document Format printDoc(Document doc, PrintStream pstrm)

Here you can find the source of printDoc(Document doc, PrintStream pstrm)

Description

print Doc

License

Open Source License

Declaration

static void printDoc(Document doc, PrintStream pstrm) 

Method Source Code

//package com.java2s;
/*  it under the terms of the GNU General Public License as published by    */

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Attr;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Document;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Comment;
import org.w3c.dom.Text;
import java.io.PrintStream;

public class Main {
    static private final String ELEM_ENDIENT = "\t";

    static void printDoc(Document doc, PrintStream pstrm) {
        String version = "1.0"; // doc.getXmlVersion();
        String encoding = null; // doc.getXmlEncoding();

        pstrm.print("<?xml version=\"" + version + "\"");

        if (encoding == null) {
            pstrm.println("?>");
        } else {/*w ww  . j a v  a  2  s . co m*/
            pstrm.println(" encoding=\"" + encoding + "\"?>");
        }

        printPI(doc, pstrm);
        pstrm.println("");
        printDoctype(doc, pstrm);
        pstrm.println("\n");
        printElem(doc.getDocumentElement(), "", true, pstrm);
    }

    static private void printPI(Document doc, PrintStream pstrm) {
        NodeList nodes = doc.getChildNodes();

        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);

            if (node instanceof ProcessingInstruction) {
                ProcessingInstruction pi = (ProcessingInstruction) node;
                pstrm.println("<?" + pi.getTarget() + " " + pi.getData()
                        + "?>");
            }
        }
    }

    static private void printDoctype(Document doc, PrintStream pstrm) {
        if (doc.getDoctype() != null) {
            pstrm.println("<!DOCTYPE " + doc.getDoctype().getName());

            String pubid = doc.getDoctype().getPublicId();

            if (pubid != null) {
                pstrm.print("\tPUBLIC \"" + pubid + "\"");
            }

            String sysid = doc.getDoctype().getSystemId();

            if (sysid != null) {
                if (pubid != null) {
                    pstrm.println("");
                }
                pstrm.print("\tSYSTEM \"" + sysid + "\"");
            }

            pstrm.print(">");
        }
    }

    static private void printElem(Element elem, String endient,
            boolean toplevel, PrintStream pstrm) {
        if (elem == null) {
            return;
        }

        pstrm.print(endient + "<" + elem.getTagName());
        printAttrs(elem, endient, pstrm);
        int count = printChildElems(elem, endient, toplevel, pstrm);
        if (count == 0) {
            String text = elem.getNodeValue();

            if (is_empty(text) == false) {
                pstrm.print(text);
            }

            pstrm.println("</" + elem.getTagName() + ">");
        } else {
            pstrm.println(endient + "</" + elem.getTagName() + ">");
        }
    }

    static private void printAttrs(Element elem, String endient,
            PrintStream pstrm) {
        NamedNodeMap attrs = elem.getAttributes();

        if (attrs.getLength() == 0) {
            pstrm.print(">");
            return;
        }

        Attr attr = (Attr) attrs.item(0);

        if (attrs.getLength() == 1) {
            pstrm.print(" " + attr.getName() + "=\"" + attr.getValue()
                    + "\">");
            return;
        }

        endient += ELEM_ENDIENT;

        for (int i = 0; i < attrs.getLength(); i++) {
            attr = (Attr) attrs.item(i);
            pstrm.print("\n" + endient + attr.getName() + "=\""
                    + attr.getValue() + "\"");
        }

        pstrm.print(">");
    }

    static private int printChildElems(Element elem, String endient,
            boolean toplevel, PrintStream pstrm) {
        endient += ELEM_ENDIENT;

        int count = 0;
        boolean after_comment = false;

        NodeList nodes = elem.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);

            if (node instanceof Element) {
                if (after_comment == false && (toplevel || count == 0)) {
                    pstrm.println("");
                }
                after_comment = false;
                printElem((Element) node, endient, false, pstrm);
                count++;
            } else if (node instanceof Comment) {
                if (toplevel && count != 0) {
                    after_comment = true;
                    pstrm.println("");
                }
                Comment comm = (Comment) node;

                String data = comm.getData();
                String commEnd;

                if (data.indexOf('\n') == -1 && data.indexOf('\r') == -1) {
                    commEnd = "-->";
                } else {
                    commEnd = "\n" + endient + "-->";
                }

                String head = "\n";
                if (toplevel) {
                    head = "";
                }

                pstrm.println(head + endient + "<!--" + data + commEnd);
            } else if (node instanceof Text) { // && elem.getTagName().equals("header") ) {
                pstrm.print(node.getNodeValue());
            }
        }

        return count;
    }

    static boolean is_empty(String str) {
        return (str == null || str.length() == 0);
    }
}

Related

  1. prettyPrintXml(Document doc)
  2. prettyPrintXml(Document document)
  3. prettyPrintXMLDocument(Node node)
  4. print(Document d)
  5. printDoc(Document doc)
  6. printDoctype(Document doc, PrintStream pstrm)
  7. printDocument(Document d)
  8. printDocument(Document doc, boolean prettyPrint)
  9. printDocument(Document doc, OutputStream out)