Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.StringTokenizer;

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

public class Main {
    /**
     * Remove an element using XPath-quotation expressions. Path must not including
     * the context element, path elements can be separated by / or .,
     * and namespace is NOT supported.
     * @param context Element to start the search from, cannot be null.
     * @param path XPath-quotation expression, cannot be null.
     * @return the removed element if there are matches, otherwise
     * return null.
     */
    public static Element removeElementByPath(Element context, String path) {
        Element cur = context;
        StringTokenizer tokens = new StringTokenizer(path, "/");
        String name = null;
        while (tokens.hasMoreTokens()) {
            name = tokens.nextToken();
            cur = getElementByPath(cur, name);
            if (cur == null) {
                return null;
            }
        }
        if (name != null) {
            Element parent = (Element) cur.getParentNode();
            return removeChildElement(parent, name);
        }
        return null;
    }

    /**
     * Find an element using XPath-quotation expressions. Path must not including
     * the context element, path elements can be separated by / or .,
     * and namespace is NOT supported.
     * @param context Element to start the search from, cannot be null.
     * @param path XPath-quotation expression, cannot be null.
     * @return the first matched element if there are matches, otherwise
     * return null.
     */
    public static Element getElementByPath(Element context, String path) {
        Element cur = context;
        StringTokenizer tokens = new StringTokenizer(path, "/");

        while (tokens.hasMoreTokens()) {
            String name = tokens.nextToken();

            cur = getChildElement(cur, name);
            if (cur == null) {
                return null;
            }
        }
        return cur;
    }

    /**
     * Find an element using XPath-quotation expressions. Path must not including
     * the context element, path elements can be separated by / or .,
     * and namespace is NOT supported.
     * @param context Element to start the search from, cannot be null.
     * @param path XPath-quotation expression, cannot be null.
     * @param create if true, new elements are created if necessary.
     * @return the first matched element if there are matches, otherwise
     * return null.
     */
    public static Element getElementByPath(Element context, String path, boolean create) {
        Element cur = context;
        StringTokenizer tokens = new StringTokenizer(path, "/");

        while (tokens.hasMoreTokens()) {
            String name = tokens.nextToken();
            Element parent = cur;
            cur = getChildElement(cur, name);
            if (cur == null) {
                if (create) {
                    // create the element
                    Element newElement = context.getOwnerDocument().createElement(name);
                    //context.appendChild(newElement);
                    parent.appendChild(newElement);
                    cur = newElement;
                } else {
                    return null;
                }
            }
        }
        return cur;
    }

    /**
     * Removes the child elements inside the given Element
     *
     * @param element the starting element, cannot be null.
     * @param elemName the name of the child element to look for, cannot be
     * null.
     * @return the removed child element inside element, or
     * <code>null</code> if the child element is not found.
     */
    public static Element removeChildElement(Element element, String elemName) {
        NodeList list = element.getChildNodes();
        Element cur = element;
        int len = list.getLength();
        for (int i = 0; i < len; i++) {
            Node n = list.item(i);

            if (n != null) {
                if (n.getNodeType() == Node.ELEMENT_NODE) {
                    if (elemName.equals(n.getNodeName())) {
                        cur = (Element) element.removeChild(n);
                    }
                }
            }
        }
        return cur;
    }

    /**
     * Access an immediate child element inside the given Element
     *
     * @param element the starting element, cannot be null.
     * @param elemName the name of the child element to look for, cannot be
     * null.
     * @return the first immediate child element inside element, or
     * <code>null</code> if the child element is not found.
     */
    public static Element getChildElement(Element element, String elemName) {
        NodeList list = element.getChildNodes();
        int len = list.getLength();

        for (int i = 0; i < len; i++) {
            Node n = list.item(i);

            if (n.getNodeType() == Node.ELEMENT_NODE) {
                if (elemName.equals(n.getNodeName())) {
                    return (Element) n;
                }
            }
        }
        return null;
    }

    /**
     * Access an immediate child element inside the given Element
     *
     * @param element the starting element, cannot be null.
     * @param namespaceURI name space of the child element to look for,
     * cannot be null. Use "*" to match all namespaces.
     * @param elemName the name of the child element to look for, cannot be
     * null.
     * @return the first immediate child element inside element, or
     * <code>null</code> if the child element is not found.
     */
    public static Element getChildElement(Element element, String namespaceURI, String elemName) {
        NodeList list = element.getChildNodes();
        int len = list.getLength();

        for (int i = 0; i < len; i++) {
            Node n = list.item(i);

            if (n.getNodeType() == Node.ELEMENT_NODE) {
                if (elemName.equals(n.getLocalName())
                        && ("*".equals(namespaceURI) || namespaceURI.equals(n.getNamespaceURI()))) {
                    return (Element) n;
                }
            }
        }
        return null;
    }
}