Example usage for org.w3c.dom Node getNodeName

List of usage examples for org.w3c.dom Node getNodeName

Introduction

In this page you can find the example usage for org.w3c.dom Node getNodeName.

Prototype

public String getNodeName();

Source Link

Document

The name of this node, depending on its type; see the table above.

Usage

From source file:Main.java

public static Node getNodeChildByName(Node parentNode, String childNodeName) {
    Node childNode = null;/*from   w  ww  .j  a v  a 2  s.  c o  m*/
    NodeList childNodeList = parentNode.getChildNodes();
    int len = childNodeList.getLength();
    for (int i = 0; i < len; i++) {
        Node tmpChildNode = childNodeList.item(i);
        if (tmpChildNode.getNodeName().equals(childNodeName)) {
            return tmpChildNode;
        }
    }
    childNode = null;
    return childNode;
}

From source file:Main.java

/**
 * Determine whether a single child is available of a particular type.
 * @return true if the specified child element is present
 * @throws Exception if the child is present multiple times.
 *//*from  w  w w.j  av a 2  s  . c  om*/
public static boolean hasChild(Element element, String child) throws Exception {
    NodeList nodes = element.getChildNodes();
    Element ret = null;
    for (int i = 0; i < nodes.getLength(); i++) {
        Node childNode = nodes.item(i);
        if (childNode.getNodeName().equals(child) && childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (ret != null) {
                throw new Exception("Child element '" + child + "' present multiple times");
            } else {
                ret = (Element) childNode;
            }
        }
    }
    return ret != null;
}

From source file:Main.java

public static Node getDescendent(Node node, String nodeName) throws IOException {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeName().equals(nodeName)) {
            return child;
        }//w  w w.ja va  2 s  . c  o m
    }
    return null;
}

From source file:Main.java

private static Node findNode(Node node, String fieldName) {
    NodeList Nodes = node.getChildNodes();
    for (int i = 0; i < Nodes.getLength(); i++) {
        Node fieldNode = Nodes.item(i);
        String name = fieldNode.getNodeName();
        if (name != null && name == fieldName) {
            return fieldNode;
        }/*from   ww w  .j  av a 2  s .co  m*/
    }
    return null;
}

From source file:Main.java

/**
 * Find node by node while ignoring the case.
 * /*from  ww w . j  a  v a2  s  .com*/
 * @param tagName     node tag (case-insensitive).
 * @param nodes       a list of nodes to look through.
 * @return a node name if the node is found, or null otherwise.
 */
public static Node getNode(String tagName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            return node;
        }
    }

    return null;
}

From source file:Main.java

public static List<Node> getNodeListByName(Node node, String name, List<Node> finalNodeList) {
    NodeList nodeList = node.getChildNodes();
    //System.out.println("child node name: "+node.getNodeName());
    if (nodeList == null)
        return null;

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node candNode = nodeList.item(i);
        if (candNode.getNodeName().equals(name)) {
            finalNodeList.add(candNode);
        }/*w ww .j a v  a2s  .  c o m*/

        getNodeListByName(nodeList.item(i), name, finalNodeList);

    }
    return finalNodeList;
}

From source file:Main.java

private static void addAttrDirect(Map<String, Object> values, Element el) {
    NamedNodeMap attrs = el.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Node attr = attrs.item(i);
        values.put(attr.getNodeName(), attr.getNodeValue());
    }/*from   w w  w. ja  v  a 2 s. com*/
}

From source file:Main.java

/**
 * Searches parent node for matching child nodes.
 *
 * @param node     The parent node/*from   www  .jav  a2  s  .co  m*/
 * @param nodeName The child node's element name
 * @return A list of matching child Nodes
 */
public static Iterator getNodes(Node node, String nodeName) {
    ArrayList nodes = new ArrayList();
    NodeList nl = node.getChildNodes();
    int nll = nl.getLength();
    for (int i = 0; i < nll; i++) {
        Node n = nl.item(i);
        if (n.getNodeName().equals(nodeName)) {
            nodes.add(n);
        }
    }
    return nodes.iterator();
}

From source file:Main.java

private static void getChildrenByName(Node node, String name, Collection<Node> nodes) {
    NodeList list = node.getChildNodes();
    for (int i = 0, len = list.getLength(); i < len; i++) {
        Node child = list.item(i);
        if (child.getNodeName().equalsIgnoreCase(name)) {
            nodes.add(child);/* w w  w .  ja  va 2 s .  co  m*/
        }
    }
}

From source file:Main.java

public static String nodeToString(Node node) {
    String s = node.getNodeName() + "( ";
    NamedNodeMap map = node.getAttributes();
    if (map != null) {
        for (int i = 0; i < map.getLength(); i++) {
            s += map.item(i).getNodeName() + "=" + map.item(i).getNodeValue() + " ";
        }/* w w  w.ja v  a  2 s .  co m*/
    }
    return s += " )";
}