Java XML Child Get getChild(Node node, String name, String attrName, String attrValue)

Here you can find the source of getChild(Node node, String name, String attrName, String attrValue)

Description

Gets a child element of a node by name and attribute value.

License

Open Source License

Parameter

Parameter Description
node Node.
name Name.
attrName Attribute name.
attrValue Attribute value.

Return

First child element with given tag name and given attribute value, or null if none found.

Declaration


public static Element getChild(Node node, String name, String attrName, String attrValue) 

Method Source Code

//package com.java2s;
/*   Please see the license information at the end of this file. */

import org.w3c.dom.*;

public class Main {
    /**   Gets a child element of a node by name.
     *// ww w  . j  a v a2  s.  co  m
     *   @param   node   Node.
     *
     *   @param   name   Name.
     *
     *   @return         First child element with given tag name, or
     *               null if none found.
     */

    public static Element getChild(Node node, String name) {
        NodeList children = node.getChildNodes();
        int numChildren = children.getLength();
        for (int i = 0; i < numChildren; i++) {
            Node child = children.item(i);
            if (child.getNodeType() != Node.ELEMENT_NODE)
                continue;
            if (child.getNodeName().equals(name))
                return (Element) child;
        }
        return null;
    }

    /**   Gets a child element of a node by name and attribute value.
     *
     *   @param   node      Node.
     *
     *   @param   name      Name.
     *
     *   @param   attrName   Attribute name.
     *
     *   @param   attrValue   Attribute value.
     *
     *   @return            First child element with given tag name and
     *                  given attribute value, or null if none found.
     */

    public static Element getChild(Node node, String name, String attrName, String attrValue) {
        NodeList children = node.getChildNodes();
        int numChildren = children.getLength();
        for (int i = 0; i < numChildren; i++) {
            Node child = children.item(i);
            if (child.getNodeType() != Node.ELEMENT_NODE)
                continue;
            if (child.getNodeName().equals(name)) {
                Element el = (Element) child;
                if (attrValue.equals(el.getAttribute(attrName)))
                    return el;
            }
        }
        return null;
    }
}

Related

  1. getChild(final Element root, final String name)
  2. getChild(final Element root, final String name)
  3. getChild(Node n, String name)
  4. getChild(Node node, String name)
  5. getChild(Node node, String name)
  6. getChild(Node parent, String name)
  7. getChild(Node parent, String name)
  8. getChildElements(Element e)
  9. getChildElements(Element e)