Java XML Has Child hasChildNode(Node parentNode, String nodeName)

Here you can find the source of hasChildNode(Node parentNode, String nodeName)

Description

Determine if the parent node contains a child node with matching name

License

Apache License

Parameter

Parameter Description
parentNode - the parent node
nodeName - name of child node to match

Return

boolean value to indicate whether the parent node contains matching child node

Declaration

public static boolean hasChildNode(Node parentNode, String nodeName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 IBM Corp.//  w w w .  ja v a  2  s .  co  m
 *
 * 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.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Determine if the parent node contains a child node with matching name
     * @param parentNode - the parent node
     * @param nodeName - name of child node to match 
     * @return boolean value to indicate whether the parent node contains matching child node
     */
    public static boolean hasChildNode(Node parentNode, String nodeName) {
        return getChildNode(parentNode, nodeName, null) != null ? true : false;
    }

    /**
     * Get the matching child node
     * @param parentNode - the parent node
     * @param name - name of child node to match 
     * @param value - value of child node to match, specify null to not match value
     * @return the child node if a match was found, null otherwise
     */
    public static Node getChildNode(Node parentNode, String name, String value) {
        List<Node> matchingChildren = getChildren(parentNode, name, value);
        return (matchingChildren.size() > 0) ? matchingChildren.get(0) : null;
    }

    /**
     * Get all matching child nodes
     * @param parentNode - the parent node
     * @param name - name of child node to match 
     * @param value - value of child node to match, specify null to not match value
     * @return matching child nodes
     */
    private static List<Node> getChildren(Node parentNode, String name, String value) {
        List<Node> childNodes = new ArrayList<Node>();
        if (parentNode == null || name == null) {
            return childNodes;
        }

        if (parentNode.getNodeType() == Node.ELEMENT_NODE && parentNode.hasChildNodes()) {
            NodeList children = parentNode.getChildNodes();
            for (int i = 0; i < children.getLength(); i++) {
                Node child = children.item(i);
                if (child != null && name.equals(child.getNodeName())
                        && (value == null || value.equals(child.getTextContent()))) {
                    childNodes.add(child);
                }
            }
        }

        return childNodes;
    }
}

Related

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