Java XML First Child Element getDirectChildNode(Node node, String name)

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

Description

get Direct Child Node

License

Open Source License

Declaration

public static Node getDirectChildNode(Node node, String name) 

Method Source Code


//package com.java2s;
import java.util.LinkedList;
import java.util.List;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static Node getDirectChildNode(Node node, String name) {
        Node targetNode = null;/*from w  w  w. j  a v  a 2s  . c om*/

        List<Node> nodes = getDirectChildNodes(node, name);
        if (nodes.size() == 1) {
            targetNode = nodes.get(0);
        }

        return targetNode;
    }

    public static List<Node> getDirectChildNodes(Node node, String name) {
        LinkedList<Node> nodes = new LinkedList<Node>();

        NodeList childList = node.getChildNodes();
        for (int childIndex = 0; childIndex < childList.getLength(); childIndex++) {
            Node child = childList.item(childIndex);
            if (child.getNodeName().equals(name)) {
                nodes.add(child);
            }
        }

        return nodes;
    }
}

Related

  1. getDirectChildElements(Node fNode, String localName, String namespace)
  2. getDirectChildElementsByTag(Element node, String tag)
  3. getDirectChildElementValue(Element p_rootElement, String p_elementName)
  4. getDirectChildElementValues(Element p_rootElement, String p_elementName)
  5. getDirectChildNamedElements( Element parent, String name)
  6. getDirectChildNodes(final Node parent, final String... nodeNames)
  7. getDirectChildNodes(Node node, String name)
  8. getFirstChild(Element e)
  9. getFirstChild(Element e, String nsUri, String local)