Android XML Node Child Check isInstance(Node node, String namespace, String name)

Here you can find the source of isInstance(Node node, String namespace, String name)

Description

Test if a node matches a namespace/name criteria, handling a null as match any.

License

LGPL

Parameter

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.

Return

True if the element matched.

Declaration

public static boolean isInstance(Node node, String namespace,
        String name) 

Method Source Code

//package com.java2s;
/*//from  ww w  .ja va2 s  . c  o 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;

public class Main {
    /**
     * 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;
    }
}

Related

  1. hasChild(Node node, String namespace, String name)
  2. hasChildElement(NodeList list)
  3. hasTextContent(Node child)
  4. isDescendantOrSelf(Node ctx, Node descendantOrSelf)