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

/**
 * @param child/*from w w w.j a v  a 2  s  .  c om*/
 * @return the single child element or null
 * @throws Exception if the child is present multiple times
 */
public static Element getChild(Element element, String child) throws Exception {
    NodeList nodes = element.getChildNodes();
    Element ret = null;
    for (int i = 0; i < nodes.getLength(); i++) {
        Node childNode = nodes.item(i);
        if (childNode.getNodeName().equalsIgnoreCase(child) && childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (ret != null) {
                throw new Exception("Child element '" + child + "' present multiple times");
            } else {
                ret = (Element) childNode;
            }
        }
    }
    if (ret == null) {
        return null;
    } else {
        return ret;
    }
}

From source file:Main.java

private static void traverseAttributes(JsonObject childJson, Node childNode) {
    NamedNodeMap attrNodeMap = childNode.getAttributes();
    for (int j = 0; j < attrNodeMap.getLength(); j++) {
        Node attrNode = attrNodeMap.item(j);
        childJson.addProperty("-" + attrNode.getNodeName(), attrNode.getNodeValue());
    }/*from w  ww . j a  v a2 s . c om*/
}

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  av a  2s. c  o m
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            removeAll(list.item(i), nodeType, name);
        }
    }
}

From source file:Main.java

/**
 * Find node by name recursively./* w  ww.ja  v  a  2  s . c o  m*/
 * @param node Node
 * @param nodeName String
 * @return Node
 */
public static Node findNode(Node node, String nodeName) {
    if (nodeName.equals(node.getNodeName()))
        return node;
    org.w3c.dom.NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node ret = findNode(children.item(i), nodeName);
        if (ret != null)
            return ret;
    }
    return null;
}

From source file:Main.java

/**
 * @param name The name of the child elements you want
 * @return a List of child Elements// w ww . j  a v  a  2s .c  om
 */
public static List<Element> getChildren(Element element, String name) throws Exception {
    NodeList nodes = element.getChildNodes();
    ArrayList<Element> ret = new ArrayList<Element>(nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {
        Node childNode = nodes.item(i);
        if (childNode.getNodeName().equals(name) && childNode.getNodeType() == Node.ELEMENT_NODE) {
            ret.add((Element) childNode);
        }
    }
    return ret;
}

From source file:Main.java

/**
 * Scans a node and all of its children for nodes of a particular type.
 * /*from  w w w . j a  v  a 2  s  . c  om*/
 * @param parent
 *            The parent node
 * @param nodeName
 *            The node name to search for
 * @return a List of all the nodes found matching the nodeName under the parent
 */
public static List<Node> getNodes(final Node parent, final String nodeName) {
    final List<Node> nodes = new ArrayList<Node>();
    final NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        final Node child = children.item(i);

        if (child.getNodeName().equals(nodeName)) {
            nodes.add(child);
        } else {
            nodes.addAll(getNodes(child, nodeName));
        }
    }
    return nodes;
}

From source file:Main.java

/**
 * Get a child element with a specific name.
 *
 * @param name Name of the requested child element.
 * @param elem Parent element.//from w w  w  .jav  a2  s  .c  om
 *
 * @return The first child element with the given name, or null if none found.
 */
public static Element getChildElement(String name, Element elem) {

    NodeList l = elem.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);
        if (n instanceof Element && n.getNodeName().equals(name))
            return (Element) n;
    }
    return null;
}

From source file:Main.java

public static HashMap<String, String> getElementsByTagNameHashMap(String sTag, Element eElement) {
    String value = null;/*from   w  w  w. j  a v a2  s  . c o  m*/
    HashMap<String, String> map = new HashMap<String, String>();
    NodeList nlList = eElement.getElementsByTagName(sTag);
    if (nlList != null && nlList.item(0) != null) {
        NodeList nodes = nlList.item(0).getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            map.put(node.getNodeName(), node.getTextContent());
        }
    }
    return map;
}

From source file:Main.java

/**
 * Gets XML node name./* www.  j  ava  2s  .  c o m*/
 *
 * @param node node.
 * @return node name.
 */
public static String getSimpleName(final Node node) {
    return node.getLocalName() == null ? node.getNodeName().substring(node.getNodeName().indexOf(':') + 1)
            : node.getLocalName();
}

From source file:Main.java

public static Node getNodeByName(Node parent, String name) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeName().equals(name)) {
            return child;
        }//from  w  w w.j av  a2s . co  m
    }
    return null;
}