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 List<Node> getChildNodes(Node node, String... elements) {
    List<Node> result = new ArrayList<Node>();
    NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        for (String element : elements) {
            if (element.equals(childNode.getNodeName())) {
                result.add(childNode);//w w w  .  ja v a  2 s  . co m
            }
        }
    }
    return result;
}

From source file:Main.java

/**
 * Searches parent node for matching child nodes and collects and returns
 * their values./*w  w  w.  j a v a 2 s.  co  m*/
 *
 * @param node     The parent node
 * @param elemName The matching child node element name
 * @return List of node values
 */
public static Enumeration getChildrenNodeValues(Node node, String elemName) {
    Vector vect = new Vector();
    NodeList nl = node.getChildNodes();
    int n = nl.getLength();
    for (int i = 0; i < n; i++) {
        Node nd = nl.item(i);
        if (nd.getNodeName().equals(elemName)) {
            Node child = nd.getFirstChild();
            if (child == null) {
                vect.add("");
            } else {
                vect.add(child.getNodeValue());
            }
        }
    }
    return vect.elements();
}

From source file:Main.java

/**
 * Returns a child node that has the given node name and give attribute name
 * and value.//  w  w w.ja  v a 2s.c om
 */
public static Node getNamedChildNode(Node parentNode, String childNodeName, String attrName, String attrValue) {
    NodeList children = parentNode.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (childNodeName.equalsIgnoreCase(node.getNodeName())) {
            if (getNodeAttributeValue(node, attrName).equalsIgnoreCase(attrValue))
                return (node);
        }
    }
    return (null);
}

From source file:Main.java

public static void removeAll(Node node, short nodeType, String name) {
    if (node.getNodeType() == nodeType && (name == null || node.getNodeName().equals(name))) {
        node.getParentNode().removeChild(node);
    } else {/*w ww .j  a  v a2s.  com*/
        // Visit the children
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            removeAll(list.item(i), nodeType, name);
        }
    }
}

From source file:Main.java

public static Element getChildElement(Element elt, String name) {
    NodeList nodes = elt.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE)
            if (name.equals(node.getNodeName()))
                return (Element) node;
    }/*from   w w  w  .  j  av a 2  s.c  o m*/
    return null;
}

From source file:Main.java

/**
 * Returns the occurrences of a given node/element. Checks whether the previous/following
 * elements have the same name.//from   w ww . ja  va  2  s.c  o m
 * 
 * @param node The node whose occurrences should be determined.
 * @return      The occurrences of node. Greater than or equal to 1.
 */
public static int getOccurs(Node node) {
    int occurs = 1;
    String nodeName = node.getNodeName();

    Node prevSib = node.getPreviousSibling();
    while (prevSib != null) {
        if (prevSib.getNodeName().compareTo(nodeName) == 0 && prevSib.getNodeType() == Node.ELEMENT_NODE) {
            occurs++;
            prevSib = prevSib.getPreviousSibling();
        } else {
            prevSib = null;
        }
    }

    Node nextSib = node.getNextSibling();
    while (nextSib != null) {
        if (nextSib.getNodeName().compareTo(nodeName) == 0 && nextSib.getNodeType() == Node.ELEMENT_NODE) {
            occurs++;
            nextSib = nextSib.getNextSibling();
        } else {
            nextSib = null;
        }
    }

    return occurs;
}

From source file:Main.java

public static List<Element> getChildElementsByTagName(Element element, String name) {
    List<Element> result = new ArrayList<Element>();

    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node instanceof Element) {
            String nodeName = node.getNodeName();
            if (nodeName != null) {
                if (nodeName.equals(name)) {
                    result.add((Element) childNodes.item(i));
                }/*from   ww  w.j  a  v  a  2  s  .c  o  m*/
            }

        }
    }

    if (result.size() == 0) {
        result = null;
    }
    return result;
}

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.  ja  v  a2 s.  c  om*/
 * @param element The node to read from
 * @param nodeName String The name of the node
 * @param caseSensitive If true, the node name searcher will be case sensitive
 * @return Node the located node or null if one was not found
 */

public static Node getChildNodeByName(Node element, CharSequence nodeName, boolean caseSensitive) {
    if (element == null)
        return null;
    if (nodeName == null)
        return null;
    final String name = nodeName.toString().trim();
    if (name.isEmpty())
        return null;
    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[] getChildrenByName(Element element, String paramString) {
    NodeList localNodeList = element.getChildNodes();
    int i = localNodeList.getLength();
    LinkedList<Node> nodes = new LinkedList<Node>();
    for (int j = 0; j < i; ++j) {
        Node localNode = localNodeList.item(j);
        if ((localNode.getNodeType() != 1) || (!localNode.getNodeName().equals(paramString)))
            continue;
        nodes.add(localNode);/*  ww  w. ja v a2 s . c  om*/
    }
    return (Element[]) nodes.toArray(new Element[nodes.size()]);
}

From source file:Main.java

private static String getXPath(Node root, String elementName) {
    for (int i = 0; i < root.getChildNodes().getLength(); i++) {
        Node node = root.getChildNodes().item(i);
        if (node instanceof Element == false) {
            return null;
        }// www.  jav a 2 s .c  om
        if (node.getNodeName().equals(elementName)) {
            return "/" + node.getNodeName();
        } else if (node.getChildNodes().getLength() > 0) {
            String xpath = getXPath(node, elementName);
            if (xpath != null) {
                return "/" + node.getNodeName() + xpath;
            }
        }
    }
    return null;
}