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

static public Element selectSingleElement(Element element, String xpathExpression) throws Exception {
    if (xpathExpression.indexOf("/") == -1) {
        NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(xpathExpression)) {
                return (Element) node;
            }//from  w w  w  . j av a2  s .co m
        }
        //  NodeList nodes = element.getElementsByTagName(xpathExpression);
        //  if (nodes.getLength() > 0) {
        //      return (Element) nodes.item(0);
        //  } else {
        return null;
        //  }
    } else {
        XPath xpath = XPathFactory.newInstance().newXPath();
        Element node = (Element) xpath.evaluate(xpathExpression, element, XPathConstants.NODE);
        return node;
    }
}

From source file:Main.java

/**
 * Search a child node by name /*from   w w  w  . java2  s.co m*/
 * 
 * @param parent   the parent node
 * @param nodeName   the node name for searching
 * @return         Node with the specified name
 * @see            Node
 * @throws Exception
 */
public static Node findChildNodeByName(Node parent, String nodeName) throws Exception {

    Node child = null;
    Node node = parent.getFirstChild();
    while (node != null) {
        if (node.getNodeName().equals(nodeName)) {
            child = node;
            break;
        }
        node = node.getNextSibling();
    }

    return child;
}

From source file:Main.java

static private void getMatchingNodes(Node node, String[] nodePath, int cur, List<Node> res) {
    if (cur < 0 || cur >= nodePath.length)
        return;/*  w ww . j  a  v a2s  . c o m*/
    boolean last = (cur == nodePath.length - 1);
    String name = nodePath[cur];
    if (node.hasChildNodes()) {
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node c = children.item(i);
            if (name.equals(c.getNodeName())) {
                if (last) {
                    res.add(c);
                } else {
                    getMatchingNodes(c, nodePath, cur + 1, res);
                }
            }
        }
    }
}

From source file:Main.java

public static List<Element> getChildElements(Element elt, String name) {
    List<Element> list = new ArrayList<Element>();

    NodeList nodes = elt.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node child = nodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE)
            if (name.equals(child.getNodeName()))
                list.add((Element) child);
    }//from  w w w  .j  a  va  2s  .c o  m
    return list;
}

From source file:Main.java

public static boolean nodeHasNameNamespace(Node node, String name, String namespaceURI) {
    String localName = node.getNodeName();

    String pre = node.getPrefix();
    if (pre != null) {
        localName = localName.substring(pre.length() + 1);
    }//from  w w  w.j av  a2  s  .co  m

    if (namespaceURI != null) {
        if (!namespaceURI.equals(node.getNamespaceURI())) {
            return false;
        }
    }

    return name.equals(localName);
}

From source file:Main.java

/**
 * Indicates if the passed node has a child node of the passed name 
 * @param element The node to inspect//w w  w . jav  a  2 s . co m
 * @param nodeName The name of the child node to look for
 * @param caseSensitive true for a case sensitive node, false for case insensitive
 * @return true if the named child node exists, false otherwise
 */
public static boolean hasChildNodeByName(Node element, CharSequence nodeName, boolean caseSensitive) {
    if (element == null)
        return false;
    if (nodeName == null)
        return false;
    final String name = nodeName.toString().trim();
    if (name.isEmpty())
        return false;
    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 true;
        } else {
            if (node.getNodeName().equalsIgnoreCase(name))
                return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * Returns the first direct child element for the given parent node, which
 * matches the given tagName (probably faster than retrieving all children first).
 * @param parent the parent node under which to search for the element
 * @param tagName the tag name of the element to find
 * @return Element - the first found Element or null if no such Element is present
 *//*from  w ww.  j a  v  a  2s. com*/
public static Element findFirstChildElement(Node parent, String tagName) {
    Node n = parent.getFirstChild();
    do {
        if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().equals(tagName))) {
            return (Element) n;
        }
        // Android Workaround
        try {
            n = n.getNextSibling();
        } catch (IndexOutOfBoundsException e) {
            n = null;
        }
    } while (n != null);
    return null;
}

From source file:Main.java

/**
 * @param node//from  w  ww  .  ja va  2  s  .  c  o m
 * @param name
 * @return the first child node with the given name or <code>null</code>
 * if none found
 */
public static Node findChild(Node node, String name) {
    NodeList nl = node.getChildNodes();
    int len = nl.getLength();
    Node child;
    for (int i = 0; i < len; i++) {
        child = nl.item(i);
        if (name.equals(child.getNodeName())) {
            return child;
        }
    }
    return null;
}

From source file:Main.java

public static Node getPrevious(Node current, boolean sameName) {
    String name = null;//from ww  w . j a va 2  s . c o  m
    if (sameName) {
        name = current.getNodeName();
    }

    int type = current.getNodeType();
    return getPrevious(current, name, type);

}

From source file:Main.java

public static String getSubTagValue(Element root, String tagName, String subTagName) {
    String returnString = "";
    NodeList list = root.getElementsByTagName(tagName);
    for (int loop = 0; loop < list.getLength(); loop++) {
        Node node = list.item(loop);
        if (node != null) {
            NodeList children = node.getChildNodes();
            for (int innerLoop = 0; innerLoop < children.getLength(); innerLoop++) {
                Node child = children.item(innerLoop);
                if ((child != null) && (child.getNodeName() != null)
                        && (child.getNodeName().equals(subTagName))) {
                    Node grandChild = child.getFirstChild();
                    if (grandChild.getNodeValue() != null) {
                        return grandChild.getNodeValue();
                    }/*from w w  w  .  ja va 2s  . c  o  m*/
                }
            }
        }
    }
    return returnString;
}