Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.PrintStream;

import org.w3c.dom.CDATASection;

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class Main {
    /**
     * Used for debuging
     * 
     * @param parent
     *            Element
     * @param out
     *            PrintStream
     * @param deep
     *            boolean
     * @param prefix
     *            String
     */
    public static void printChildElements(Element parent, PrintStream out, boolean deep, String prefix) {
        out.print(prefix + "<" + parent.getNodeName());
        NamedNodeMap attrs = parent.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 = getElementTextValueDeprecated(parent);
        String data = parent.getNodeValue();
        if (data != null && data.trim().length() > 0) {
            out.println(prefix + "\t" + data);
        }

        data = getElementCDataValue(parent);
        if (data != null && data.trim().length() > 0) {
            out.println(prefix + "\t<![CDATA[" + data + "]]>");
        }

        NodeList nodes = parent.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            node = nodes.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                if (deep) {
                    printChildElements((Element) node, out, deep, prefix + "\t");
                } else {
                    out.println(prefix + node.getNodeName());
                }
            }
        }

        out.println(prefix + "</" + parent.getNodeName() + ">");
    }

    /**
     * Gets CDATA value of an element
     * 
     * @param e
     *            the element
     * @return CDATA value of element e
     */
    public static String getElementCDataValue(Element e) {
        CDATASection text = getElementCDataNode(e);
        if (text != null) {
            return text.getData().trim();
        } else {
            return null;
        }
    }

    /**
     * Returns element's CDATA Node
     * 
     * @param element
     *            the element which CDATA node is returned
     * @return CDATA node
     */
    public static CDATASection getElementCDataNode(Element element) {
        return (CDATASection) getChildNodeByType(element, Node.CDATA_SECTION_NODE);
    }

    private static Node getChildNodeByType(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;
    }
}