Java tutorial
//package com.java2s; /* * Jitsi, the OpenSource Java VoIP and Instant Messaging client. * * Distributable under LGPL license. * See terms of license at gnu.org. */ import java.io.*; import org.w3c.dom.*; public class Main { /** * Whenever you'd need to print a configuration node and/or its children. * * @param root the root node to print. * @param out the print stream that should be used to outpu * @param recurse boolean * @param prefix String */ public static void printChildElements(Element root, PrintStream out, boolean recurse, String prefix) { out.print(prefix + "<" + root.getNodeName()); NamedNodeMap attrs = root.getAttributes(); Node node; for (int i = 0; i < attrs.getLength(); i++) { node = attrs.item(i); out.print(" " + node.getNodeName() + "=\"" + node.getNodeValue() + "\""); } out.println(">"); String data = getText(root); if (data != null && data.trim().length() > 0) out.println(prefix + "\t" + data); data = getCData(root); if (data != null && data.trim().length() > 0) out.println(prefix + "\t<![CDATA[" + data + "]]>"); NodeList nodes = root.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { if (recurse) printChildElements((Element) node, out, recurse, prefix + "\t"); else out.println(prefix + node.getNodeName()); } } out.println(prefix + "</" + root.getNodeName() + ">"); } /** * Extracts the String content of a TXT element. * * @param parentNode the node containing the data that we'd like to get. * @return the string contained by the node or null if none existed. */ public static String getText(Element parentNode) { Text text = getTextNode(parentNode); return (text == null) ? null : text.getData(); } /** * Extract the CDATA content of the specified element. * @param element the element whose data we need * @return a String containing the CDATA value of element. */ public static String getCData(Element element) { CDATASection text = getCDataNode(element); return (text == null) ? null : text.getData().trim(); } /** * Returns element's TEXT child node (if it has one). * @param element the element whose TEXT we need to get. * @return a <tt>Text</tt> object containing the specified element's * text content. */ public static Text getTextNode(Element element) { return (Text) getChildByType(element, Node.TEXT_NODE); } /** * Returns element's CDATA child node (if it has one). * @param element the element whose CDATA we need to get. * @return a CDATASection object containing the specified element's CDATA * content */ public static CDATASection getCDataNode(Element element) { return (CDATASection) getChildByType(element, Node.CDATA_SECTION_NODE); } /** * Returns first of the <tt>element</tt>'s child nodes that is of type * <tt>nodeType</tt>. * @param element the element whose child we need. * @param nodeType the type of the child we need. * @return a child of the specified <tt>nodeType</tt> or null if none * was found. */ public static Node getChildByType(Element element, short nodeType) { if (element == null) return null; NodeList nodes = element.getChildNodes(); if (nodes == null || nodes.getLength() < 1) return null; Node node; String data; for (int i = 0; i < nodes.getLength(); i++) { node = nodes.item(i); short type = node.getNodeType(); if (type == nodeType) { if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE) { data = ((Text) node).getData(); if (data == null || data.trim().length() < 1) continue; } return node; } } return null; } }