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[] getChildrenByName(Element parentElement, String childrenName) {
    NodeList nl = parentElement.getChildNodes();
    int max = nl.getLength();
    LinkedList<Node> list = new LinkedList<Node>();
    for (int i = 0; i < max; i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(childrenName)) {
            list.add(n);/*from  ww w . j  a v a 2  s  .  com*/
        }
    }
    return list.toArray(new Element[list.size()]);
}

From source file:Main.java

/**
 * @param element/*from ww  w. j  a  va  2 s  .c om*/
 * @param map
 * @return
 */
private static boolean fillAttsIntoMap(Element element, Map<String, String> map) {
    /*
     * this case is applicable only if there are no child elements
     */
    if (element.getChildNodes().getLength() > 0) {
        return false;
    }

    /*
     * in case this is not a special case, it will not have attributes, and
     * hence we are safe
     */
    NamedNodeMap attrs = element.getAttributes();
    int n = attrs.getLength();
    for (int i = 0; i < n; i++) {
        Node att = attrs.item(i);
        map.put(att.getNodeName(), att.getNodeValue());
    }
    return true;
}

From source file:Main.java

public static String getTagName(Node labelNode) {
    if (labelNode.getNodeType() == Node.ELEMENT_NODE) {
        return labelNode.getNodeName();
    }//from ww w . j  a v a 2 s  .  c  om
    return null;
}

From source file:Main.java

public static Node findNode(Node node, String name) {
    String nodeName = node.getNodeName();
    nodeName = nodeName.substring((nodeName.indexOf(":") != -1 ? nodeName.indexOf(":") + 1 : 0));
    name = name.substring((name.indexOf(":") != -1 ? name.indexOf(":") + 1 : 0));
    if (nodeName.equals(name)) {
        return node;
    }//from w w w .jav  a 2 s .c  o  m
    if (node.hasChildNodes()) {
        NodeList list = node.getChildNodes();
        int size = list.getLength();

        for (int i = 0; i < size; i++) {
            Node found = findNode(list.item(i), name);
            if (found != null)
                return found;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Method to get Values within AttributeValuePair as a java.util.Set
 *///  w w w.java 2 s . c  o  m
public static Set getAttributeValuePair(Node node) {
    if (!node.getNodeName().equals(ATTR_VALUE_PAIR_NODE))
        return (null);

    Set retVal = new HashSet();
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node n = children.item(i);
        if (n.getNodeName().equalsIgnoreCase(VALUE_NODE)) {
            retVal.add(getValueOfValueNode(n));
        }
    }
    return (retVal);
}

From source file:Main.java

/**
 * Helper Method. Searches through the child nodes of a node and returns the first node with a matching name.
 * Do we need this ?//from  w  ww  .  j a  va 2s .c  om
 * @param element Element
 * @param name String
 * @param caseSensitive boolean
 * @return Node
 */

public static Node getChildNodeByName(Node element, String name, boolean caseSensitive) {
    NodeList list = element.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (caseSensitive) {
            if (node.getNodeName().equals(name))
                return node;
        } else {
            if (node.getNodeName().equalsIgnoreCase(name))
                return node;
        }
    }
    return null;
}

From source file:Main.java

public static Element getChildElement(String tagName, Element element) {
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tagName)) {
            return (Element) n;
        }/*from ww w.  j  av a 2 s.  com*/
    }
    return null;
}

From source file:Utils.java

/**
 * Search up the tree for a given node//from w  ww  .  ja  v  a  2  s.c o  m
 * @param currentNode Starting point for our search
 * @param tagName Node name to look up
 * @return matching Node (null if none)
 */
public static Node getPreviousNodeByName(Node currentNode, String tagName) {
    Node node = currentNode.getParentNode();

    while ((node != null) && (!node.getNodeName().equals(tagName))) {
        node = node.getParentNode();
    }
    return node;
}

From source file:Main.java

/**
 * This method creates a collection of child nodes from a parent node
 * @param root the parent node/*from w  w  w  . j  ava 2  s . co  m*/
 * @return Collection - childern nodes
 */
public static Collection getCollection(Node root) {
    Collection collection = new ArrayList();
    if (root != null) {
        NodeList children = root.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            String nodeName = child.getNodeName();
            if (!nodeName.equalsIgnoreCase("#comment") && !nodeName.equalsIgnoreCase("#text")) {
                if (child.getChildNodes().getLength() > 1) {
                    collection.add(child);
                } else {
                    Node textChild = child.getFirstChild();
                    if (textChild == null) {
                        // don't accept nulls
                    } else {
                        collection.add(child.getFirstChild().getNodeValue());
                    }
                }
            }
        }
    }
    return collection;
}

From source file:Main.java

static public String getNodeAttr(String tagName, String attrName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            NodeList childNodes = node.getChildNodes();
            for (int y = 0; y < childNodes.getLength(); y++) {
                Node data = childNodes.item(y);
                if (data.getNodeType() == Node.ATTRIBUTE_NODE) {
                    if (data.getNodeName().equalsIgnoreCase(attrName)) {
                        return data.getNodeValue();
                    }/*from   w  w  w . ja  v a 2 s .co m*/
                }
            }
        }
    }

    return "";
}