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 Element[] getElementsByName(Element parent, String name) {
    ArrayList resList = new ArrayList();
    NodeList nl = getNodeList(parent);
    for (int i = 0; i < nl.getLength(); i++) {
        Node nd = nl.item(i);
        if (nd.getNodeName().equals(name)) {
            resList.add(nd);/*from   w  w  w.  ja  v  a 2  s . c o  m*/
        }
    }
    Element[] res = new Element[resList.size()];
    for (int i = 0; i < resList.size(); i++) {
        res[i] = (Element) resList.get(i);
    }
    logger.debug(parent.getNodeName() + "'s children of " + name + "'s num:" + res.length);
    return res;
}

From source file:Main.java

/**
 * Search a child node by type/*from w ww .j  a  va2  s.  c o  m*/
 * 
 * @param parent   the parent node
 * @param nodeName   the node name
 * @param nodeType  the node type for searching
 * @return         Node with the specified name
 * @see            Node
 * @throws Exception
 */
public static Node findChildNodeByNameAndType(Node parent, String nodeName, String nodeType) throws Exception {
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node.getNodeName().equals(nodeName)) {
            String strType = node.getAttributes().getNamedItem("type").getNodeValue();
            if (strType.equals(nodeType))
                return node;
        }
    }
    return null;
}

From source file:Main.java

public static List<Node> getDescendents(Node node, String nodeName) throws IOException {
    List<Node> childList = new ArrayList<Node>();
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeName().equals(nodeName)) {
            childList.add(child);/*from  ww w.j  a v a  2s . c  om*/
        }
    }
    return childList;
}

From source file:Main.java

public static Element[] getElementsByName(Element parent, String name) {

    List<Node> resList = Lists.newArrayList();
    NodeList nl = getNodeList(parent);
    for (int i = 0; i < nl.getLength(); i++) {
        Node nd = nl.item(i);
        if (nd.getNodeName().equals(name)) {
            resList.add(nd);//from   ww  w . j  av  a2  s.co  m
        }
    }
    Element[] res = new Element[resList.size()];
    for (int i = 0; i < resList.size(); i++) {
        res[0] = (Element) resList.get(i);
    }

    return res;
}

From source file:Main.java

public static Node getFirstNamedNode(Node node, String name) {
    if (node.getNodeName().equalsIgnoreCase(name))
        return node;
    else {//ww  w.ja v  a  2  s.  com
        NodeList list = node.getChildNodes();
        for (int i = 0, len = list.getLength(); i < len; i++) {
            Node n = getFirstNamedNode(list.item(i), name);
            if (n != null)
                return n;
        }
        return null;
    }
}

From source file:Main.java

/**
 * Gets the tag value without namespace.
 * /*from w  w  w.  j a  va2  s  .c o m*/
 * @param n
 *            the n
 * @return the tag value without namespace
 */
public static String getTagValueWithoutNamespace(Node n) {
    if (n != null) {
        String tag = n.getNodeName();
        return tag.substring(tag.indexOf(":") + 1, tag.length());
    }
    return null;
}

From source file:Main.java

public static int getCountOfChildNodes(Node node, String nodeName) {
    int count = 0;

    if (node.getNodeType() == Node.ELEMENT_NODE) {

        NodeList nodeList = node.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node n = nodeList.item(i);
            if (n.getNodeName().equals(nodeName)) {
                count++;//from  ww w . ja v a  2 s  .c  o m
            }
        }

    }

    return count;
}

From source file:Main.java

public static HashMap<String, String> getNodeAttributesMap(Node node) {
    HashMap<String, String> map = new HashMap<String, String>();

    NamedNodeMap namedNodeMap = node.getAttributes();
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Node attribute = namedNodeMap.item(i);
        map.put(attribute.getNodeName(), attribute.getNodeValue());
    }//from  w  w  w.  j  a va 2 s  . c o  m

    return map;
}

From source file:Main.java

public static void dumpNodeDetails(Node node) throws Exception {
    NamedNodeMap attributes = node.getAttributes();
    for (int t = 0; t < attributes.getLength(); t++) {
        Node aNode = attributes.item(t);
        log.info("aName = " + aNode.getNodeName() + " : aValue = " + aNode.getNodeValue());
    }//from   w w  w . j av a  2  s .co  m
}

From source file:Main.java

public static String getAttributeValue(Node node, String attributeName) {
    for (Node child : getChildren(node)) {
        if (child.getNodeName().equalsIgnoreCase(attributeName)) {
            return child.getNodeValue();
        }/*from w ww. ja  v  a 2 s.c  om*/
    }
    return null;
}