Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {
    /**
     * A convenience method for return a string from an element.  First, this
     * method checks to see if the the specified name exists as an attribute
     * in the element.  If it does, simply returns the attribute value.
     * Then this method checks to see if the specified element has a child
     * element that matches the given name.  If it does, attempts to read
     * the text from the child node.  Otherwise, returns <code>null</code>.
     * @param element
     * @param name
     * @return String
     */
    public static String getAttributeOrChildText(Element element, String name) {
        if (element.hasAttribute(name)) {
            return element.getAttribute(name);
        }
        Element childElem = getChildElement(element, name);
        return childElem == null ? null : getChildText(childElem);
    }

    /**
     * Returns the first child element with the specified name, or null if none exists.
     * @param element
     * @param elementName
     * @return Element
     */
    public static Element getChildElement(Element element, String elementName) {
        for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
            if (node instanceof Element) {
                Element childElem = (Element) node;
                String elemName = getElementName(childElem);

                if (elementName.equals(elemName))
                    return childElem;
            }
        }

        return null;
    }

    /**
     * Returns the child text of this element, or null if there is none.
     * @param element
     * @return String
     */
    public static String getChildText(Element element) {
        return getChildText(element, null);
    }

    /**
     * Returns the child text of this element, or the default value if there is none.
     * @param element
     * @param defaultValue
     * @return Either the child text of the element or the default value if there is not child text.
     */
    public static String getChildText(Element element, String defaultValue) {
        Node childNode = element.getFirstChild();
        if (childNode == null || !(childNode instanceof Text))
            return defaultValue;

        return childNode.getNodeValue();
    }

    /**
     * Returns the element name of the given element.
     * @param element
     * @return String
     */
    public static String getElementName(Element element) {
        // When loading from disk the local name will be setup correctly, but if the local name
        // is fetched directly after calling Document.createElement() then the value will be null.
        // See the JavaDoc for more info.  To workaround this, use the complete node name.
        String elemName = element.getLocalName();
        if (elemName == null) {
            elemName = element.getNodeName();
        }
        return elemName;
    }
}