Java XML Child Node Get getChildNodeByName(Node element, CharSequence nodeName, boolean caseSensitive)

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

Description

Helper Method.

License

Apache License

Parameter

Parameter Description
element The node to read from
nodeName String The name of the node
caseSensitive If true, the node name searcher will be case sensitive

Return

Node the located node or null if one was not found

Declaration


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

Method Source Code


//package com.java2s;
/*//from  w  w  w. j a va  2 s  . c  o 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 {
    /**
     * Helper Method. Searches through the child nodes of a node and returns the first node with a matching name.
     * Do we need this ?
     * @param element The node to read from
     * @param nodeName String The name of the node
     * @param caseSensitive If true, the node name searcher will be case sensitive
     * @return Node the located node or null if one was not found
     */

    public static Node getChildNodeByName(Node element, CharSequence nodeName, boolean caseSensitive) {
        if (element == null)
            return null;
        if (nodeName == null)
            return null;
        final String name = nodeName.toString().trim();
        if (name.isEmpty())
            return null;
        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 node;
            } else {
                if (node.getNodeName().equalsIgnoreCase(name))
                    return node;
            }
        }
        return null;
    }

    /**
     * Helper Method. Case insensitive. Searches through the child nodes of a node and returns the first node with a matching name.
     * Do we need this ?
     * @param element The node to read from
     * @param name The name of the child node
     * @return Node the located node or null if one was not found
     */

    public static Node getChildNodeByName(Node element, CharSequence name) {
        return getChildNodeByName(element, name, false);
    }
}

Related

  1. getChildNode(Node parentNode, String childElementName)
  2. getChildNode(Node root, String childName)
  3. getChildNode(Node rootNode, String childName)
  4. getChildNodeByLocalName(final Node parentNode, final String localNodeName)
  5. getChildNodeByName(final Node xnode, final String name, final boolean caseSensitive)
  6. getChildNodeByName(Node node, String childName)
  7. getChildNodeByName(Node node, String name)
  8. getChildNodeByTagName(Node node, String name)
  9. getChildNodeByType(Element element, short nodeType)