Here you can find the source of hasChild(Node node, String namespace, String name)
Parameter | Description |
---|---|
node | The node to scan. |
namespace | The requested namespace, or null for no preference. |
name | The element name, or null for no preference. |
public static boolean hasChild(Node node, String namespace, String name)
//package com.java2s; /*//from www .j a v a2s . co m * Licensed under Apache License, Version 2.0 or LGPL 2.1, at your option. * -- * * Copyright 2010 Rene Treffer * * 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. * * -- * * Copyright (C) 2010 Rene Treffer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Search for availability of a certain child. * @param node The node to scan. * @param namespace The requested namespace, or null for no preference. * @param name The element name, or null for no preference. * @return True if a matching child exists. */ public static boolean hasChild(Node node, String namespace, String name) { return getFirstChild(node, namespace, name) != null; } /** * Return the first child of a node, based on name/namespace. * @param node The node to scan. * @param namespace The requested namespace, or null for no preference. * @param name The element name, or null for no preference. * @return The first matching node or null. */ public static Node getFirstChild(Node node, String namespace, String name) { NodeList childNodes = node.getChildNodes(); for (int i = 0, l = childNodes.getLength(); i < l; i++) { Node child = childNodes.item(i); if (isInstance(child, namespace, name)) { return child; } } return null; } /** * Test if a node matches a namespace/name criteria, handling a null as * match any. * @param node The node to scan. * @param namespace The requested namespace, or null for no preference. * @param name The element name, or null for no preference. * @return True if the element matched. */ public static boolean isInstance(Node node, String namespace, String name) { if (node.getNodeType() != Node.ELEMENT_NODE) { return false; } if (namespace != null && !namespace.equals(node.getNamespaceURI())) { return false; } if (name != null && !name.equals(node.getLocalName()) && !name.equals(node.getNodeName())) { return false; } return true; } }