Java XML Has Child hasChildNodeByName(Node element, CharSequence nodeName, boolean caseSensitive)

Here you can find the source of hasChildNodeByName(Node element, CharSequence nodeName, boolean caseSensitive)

Description

Indicates if the passed node has a child node of the passed name

License

Apache License

Parameter

Parameter Description
element The node to inspect
nodeName The name of the child node to look for
caseSensitive true for a case sensitive node, false for case insensitive

Return

true if the named child node exists, false otherwise

Declaration

public static boolean hasChildNodeByName(Node element, CharSequence nodeName, boolean caseSensitive) 

Method Source Code


//package com.java2s;
/*/*from  w ww .  j av a 2s  .  co  m*/
 * Copyright 2015 the original author or authors.
 *
 * Licensed 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 org.w3c.dom.*;

public class Main {
    /**
     * Indicates if the passed node has a child node of the passed name 
     * @param element The node to inspect
     * @param nodeName The name of the child node to look for
     * @param caseSensitive true for a case sensitive node, false for case insensitive
     * @return true if the named child node exists, false otherwise
     */
    public static boolean hasChildNodeByName(Node element, CharSequence nodeName, boolean caseSensitive) {
        if (element == null)
            return false;
        if (nodeName == null)
            return false;
        final String name = nodeName.toString().trim();
        if (name.isEmpty())
            return false;
        NodeList list = element.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            if (caseSensitive) {
                if (node.getNodeName().equals(name))
                    return true;
            } else {
                if (node.getNodeName().equalsIgnoreCase(name))
                    return true;
            }
        }
        return false;
    }
}

Related

  1. hasChildElements(Node node)
  2. hasChildElements(Node node)
  3. hasChildElementWithAttribute(Element element, String attributeName, String attributeValue)
  4. hasChildNode(Element root, String childNodeName)
  5. hasChildNode(Node parentNode, String nodeName)
  6. hasChildNodes(Element element, String name)
  7. hasChildNS(Node parent, String ns, String name)
  8. hasChildOfType(final Element e, final String name)
  9. hasChildren(Element e)