Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/* 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

import javax.xml.namespace.QName;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    /**
     * This is a convenience method that returns the first Element in the
     * array returned by findInSubTree(Element, QName), if any.
     * 
     * @param context
     *            The root node from which the search will be done.
     * @param qname
     *            The QName of the Element to search for.
     * @return The first occurrence of a child Element with the given name.
     *         The Element may be anywhere in the sub-tree, meaning it may
     *         not be a direct child of the context node. The only guarantee
     *         is that it is somewhere beneath the context node. The method
     *         returns null if no occurrences are found.
     * @see #findInSubTree(Element, QName)
     */
    public static Element findFirstInSubTree(final Element context, final QName qname) {
        //
        // do a complete search and return the first result (if one exists)
        //        
        final Element[] results = findInSubTree(context, qname);

        if (results.length == 0)
            return null;

        return results[0];
    }

    /**
     * Searches through an entire sub-tree for child Elements whose QName
     * matches the one given. The results are not guaranteed to be in any
     * particular order, and are <b>not</b> limited to direct children of
     * the given context node. <br>
     * <br>
     * If you want to limit your search for Elements to the direct children
     * of a given Element, use the getAllElement, getElement, and getElements
     * methods. Those guarantee that all results are directly beneath the
     * given context.
     * 
     * @param context
     *            The root node from which the search will be done.
     * @param qname
     *            The QName of the Element to search for.
     * @return An array with all occurrences of child Elements with the
     *         given name. The Elements may be anywhere in the sub-tree,
     *         meaning they may not be direct children of the context node.
     *         The only guarantee is that they are somewhere beneath the
     *         context node. The method returns an empty array if no
     *         occurrences are found.
     * @see Element#getElementsByTagNameNS(String, String)
     * @see #getAllElements(Node)
     * @see #getElement(Node, QName)
     * @see #getElements(Node, QName)
     */
    public static Element[] findInSubTree(final Element context, final QName qname) {
        final String name = qname.getLocalPart();
        final String uri = qname.getNamespaceURI();

        //
        // DOM's getElementsByTagName methods search the whole subtree
        //
        final NodeList matches = context.getElementsByTagNameNS(uri, name);
        final int length = matches.getLength();

        final Element[] asArray = new Element[length];

        for (int n = 0; n < length; ++n)
            asArray[n] = (Element) matches.item(n);

        return asArray;
    }
}