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

/**
 * Method allNodesByName.//  ww  w.j  a  v  a  2 s  . co m
 *
 * @param node
 * @param nodeName
 * @return ArrayList
 *         <p/>
 *         Purpose: given a node & tag name, returns an ArrayList of ALL nodes that
 *         have that tag. Useful when there are multiple nodes of the same. For
 *         example, used to parse an entire XML document into an array of testcases.
 */
public static ArrayList allNodesByName(Node node, String nodeName) {
    ArrayList nodes = new ArrayList();
    NodeList currNodes = node.getChildNodes();
    int length = currNodes.getLength();
    for (int i = 0; i < length; i++) {
        Node thisNode = currNodes.item(i);
        if (thisNode.getNodeName().equals(nodeName)) {
            nodes.add(thisNode);
        }
    }
    if (nodes.size() > 0) {
        return nodes;
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Returns absolute xpath for specified element, ignores namespaces
 * //from w ww . j a  va 2 s . com
 * @param element
 *           the element to create for
 * @return the elements path in its containing document
 */

public static String getElementPath(Element element) {
    Node elm = element;

    String result = elm.getNodeName() + "[" + getElementIndex(elm) + "]";
    while (elm.getParentNode() != null && elm.getParentNode().getNodeType() != Node.DOCUMENT_NODE) {
        elm = elm.getParentNode();
        result = elm.getNodeName() + "[" + getElementIndex(elm) + "]/" + result;
    }

    return "/" + result;
}

From source file:Main.java

/**
 * get the value of an Element in the Xml Document.
 * finds the first occurance of the parent element and then searches its children
 * for the first occurance of the element and retrieves its value.
 * @param root the root Element.//from ww  w.ja v  a 2s .c  om
 * @param parent the name of the parent element to search for.
 * @param elemName the name of the child element to search for.
 * @return String the element value or null if not found.
 */
public static String getSubElementValue(Element root, String parent, String elemName) {
    NodeList nl = root.getElementsByTagName(parent);
    String value = null;
    if (null == nl) {
        return (null);
    }
    Node node = nl.item(0);
    nl = node.getChildNodes();
    if (null == nl) {
        return (null);
    }

    boolean found = false;
    for (int i = 0; !found && i < nl.getLength(); ++i) {
        Node n = nl.item(i);
        if (elemName.equals(n.getNodeName())) {
            value = n.getFirstChild().getNodeValue().trim();
            found = true;
            break;
        } // if
    } // for
    return (value);
}

From source file:Main.java

public static Element[] getChildren(Element parent, String name) {
    ArrayList<Element> al = new ArrayList<Element>();
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
            al.add((Element) n);//from   w  ww  . ja  v  a  2 s . co  m
        }
    }
    return al.toArray(new Element[al.size()]);
}

From source file:Main.java

public static String getNodeValue(String tagName, 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.TEXT_NODE)
                    return data.getNodeValue();
            }/*  w w w. j a v a 2s. c  o m*/
        }
    }
    return "";
}

From source file:Main.java

static public String getNodeAttr(String attrName, Node node) {
    NamedNodeMap attrs = node.getAttributes();
    for (int y = 0; y < attrs.getLength(); y++) {
        Node attr = attrs.item(y);
        if (attr.getNodeName().equalsIgnoreCase(attrName)) {
            return attr.getNodeValue();
        }/* w w  w  .jav  a  2  s  .  c om*/
    }
    return "";
}

From source file:Main.java

public static ArrayList<HashMap<String, String>> getWhoList(Node currentNode, String tagName) {
    String result = "";
    ArrayList<HashMap<String, String>> whoArrayList = new ArrayList<HashMap<String, String>>();
    NodeList childNodeList = currentNode.getChildNodes();
    for (int i = 0; i < childNodeList.getLength(); i++) {
        Node childNode = childNodeList.item(i);
        if (childNode.getNodeName().equals(tagName)) {
            HashMap<String, String> whoMap = new HashMap<String, String>();
            NamedNodeMap attrNodeMap = childNode.getAttributes();
            Node activity = attrNodeMap.getNamedItem("activity");
            Node email = attrNodeMap.getNamedItem("email");
            Node name = attrNodeMap.getNamedItem("name");
            if (activity != null) {
                whoMap.put("activity", activity.getNodeValue());
            }/*  w w w. j ava 2s .c o  m*/
            if (email != null) {
                whoMap.put("email", email.getNodeValue());
            }
            if (name != null) {
                whoMap.put("name", name.getNodeValue());
            }
            whoArrayList.add(whoMap);
        }
    }
    return whoArrayList;
}

From source file:Main.java

private static String getNodeName(Node node) {
    String name = "";
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node curNode = nodeList.item(i);
        if ("name".equalsIgnoreCase(curNode.getNodeName())) {
            name = curNode.getTextContent().replace("\"", "");
            break;
        }/*  w  w w  .  j av a  2s .  com*/
    }

    return name;
}

From source file:Main.java

public static Set getChildNodes(Node parentNode, String childName) {
    Set retVal = new HashSet();
    NodeList children = parentNode.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node.getNodeName().equalsIgnoreCase(childName)) {
            retVal.add(node);//from w  ww .ja  v  a2 s.c  o  m
        }
    }
    return (retVal);
}

From source file:Main.java

public static Node getParent(String tagName, Node node) {
    Node parent = node.getParentNode();

    while (parent != null && !tagName.equalsIgnoreCase(parent.getNodeName())) {
        parent = parent.getParentNode();
    }//from   w w  w .j  a v  a 2  s .  co  m

    return parent;
}