Here you can find the source of printElemShallow(Element elem, String endient, PrintStream pstrm)
static void printElemShallow(Element elem, String endient, PrintStream pstrm)
//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.Comment; import org.w3c.dom.Text; import java.io.PrintStream; public class Main { static private final String ELEM_ENDIENT = "\t"; static void printElemShallow(Element elem, String endient, PrintStream pstrm) {// w w w .jav a 2 s . co m if (elem == null) { return; } String tag = elem.getTagName(); if (tag.equals("bean") == false && tag.equals("ioc") == false) { return; } pstrm.print(endient + "<" + elem.getTagName()); printAttrs(elem, endient, pstrm); int count = printMethodArgs(elem, endient, pstrm); if (count == 0) { pstrm.println(); } pstrm.println(endient + "</" + tag + ">"); } 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 printMethodArgs(Element elem, String endient, PrintStream pstrm) { endient += ELEM_ENDIENT; int count = 0; NodeList nodes = elem.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node instanceof Element) { Element child = (Element) node; String tag = child.getTagName(); if (tag.equals("method-arg")) { if (count == 0) { pstrm.println(); } printElem((Element) node, endient, true, pstrm); count++; } } } return count; } 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 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); } }