Java XML Node Name getNodes(Node module, String interfaceName, String nodeName)

Here you can find the source of getNodes(Node module, String interfaceName, String nodeName)

Description

Gets all nodes of a module that are child of a given interface and have a specific name

License

Apache License

Parameter

Parameter Description
module The module that contains the given interface
interfaceName the interface to be used for search nodes/childs
nodeName the node name to be used for select nodes as result

Return

list of Nodes that have a specific name and are part of a specific interface

Declaration

static ArrayList<Node> getNodes(Node module, String interfaceName, String nodeName) 

Method Source Code

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

import java.util.ArrayList;

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

public class Main {
    /**/*  ww w  . ja  va  2s .  c  o  m*/
     * Gets all nodes of a module that are child of a given interface and have
     * a specific name
     * @param module The module that contains the given interface
     * @param interfaceName the interface to be used for search nodes/childs
     * @param nodeName the node name to be used for select nodes as result
     * @return list of Nodes that have a specific name and are part of a
     *         specific interface
     */
    static ArrayList<Node> getNodes(Node module, String interfaceName, String nodeName) {
        Node iFace = getInterfaceNodeByName(interfaceName, module);
        NodeList childs = iFace.getChildNodes();
        ArrayList<Node> operations = new ArrayList<Node>();
        Node child;
        for (int i = 0; i < childs.getLength(); i++) {
            child = childs.item(i);
            if (child.getNodeName().equals(nodeName)) {
                operations.add(child);
            }
        }
        return operations;
    }

    /**
     * Gets an interface child node of the given node with the given name
     * @param interfaceName
     * @param node
     * @return
     */
    static Node getInterfaceNodeByName(String interfaceName, Node node) {
        ArrayList<Node> nodes = getNodesByName("Interface", node.getChildNodes());

        for (int i = 0; i < nodes.size(); i++) {
            if (getAttributeValue(nodes.get(i), "name").equals(interfaceName)) {
                return nodes.get(i);
            }
        }
        return null;
    }

    /**
     * Gets all nodes with a specific name
     * @param name
     * @param nodeList
     * @return
     */
    static ArrayList<Node> getNodesByName(String name, NodeList nodeList) {
        ArrayList<Node> nodes = new ArrayList<Node>();

        for (int i = 0; i < nodeList.getLength(); i++) {
            if (nodeList.item(i).getNodeName().equals(name)) {
                nodes.add(nodeList.item(i));
            }
        }
        return nodes;
    }

    /**
     * Gets the value of a specific attribute attached to the given node
     * @param node
     * @param attrName
     * @return
     */
    static String getAttributeValue(Node node, String attrName) {
        if (node == null)
            return null;
        ;
        NamedNodeMap map = node.getAttributes();
        if (map != null && map.getNamedItem(attrName) != null)
            return map.getNamedItem(attrName).getTextContent();
        else
            return null;
    }
}

Related

  1. getNodeName(Node node)
  2. getNodeNameIgnorePrefix(Node node)
  3. getNodeNames(Set nodes)
  4. getNodeNameWithNamespace(Node node)
  5. getNodes(Node iNode, String iNodeName)
  6. getNodes(Node node, String nodeName)
  7. getSimpleName(final Node node)
  8. getSubNodes(Node node, String subNodeName)
  9. getSubNodeValue(Node node, String nodeName, String subNodeName)