Java XML Element Get from Parent getNameToFirstNode(Node parent, String node_name)

Here you can find the source of getNameToFirstNode(Node parent, String node_name)

Description

get Name To First Node

License

LGPL

Declaration

public static Node getNameToFirstNode(Node parent, String node_name) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import org.w3c.dom.*;

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

public class Main {
    public static Node getNameToFirstNode(Node parent, String node_name) {
        if (parent == null || node_name == null)
            return null;
        Node[] nodes = getNameToNodeList(parent, node_name);
        return node_name.length() > 0 ? nodes[0] : null;
    }//from w w w.j a  va2 s . c  o m

    public static Node[] getNameToNodeList(Node parent, String node_name) {
        List<Node> nodes = new ArrayList<>();
        if (parent == null || node_name == null)
            return nodes.toArray(new Node[0]);
        NodeList list = parent.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            if (node.getNodeName().equals(node_name)) {
                nodes.add(node);
            }
        }
        return nodes.toArray(new Node[0]);
    }
}

Related

  1. getFirstElement(Node parent)
  2. getFirstNode(final Node parent, final String... path)
  3. getNamedElemValue(Element parent, String elementName)
  4. getNamedNodeList(Element parent, String containerTagName)
  5. getNamespacePrefix(Node parentNode, String preferredPrefix, String nsUri)
  6. getNameToNodeList(Node parent, String node_name)
  7. getNodeAtPosition(Node parent, int offset)
  8. getNodeBean(Node parent)
  9. getNodeByLocalName(final Node parent, final String name)