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 Node getChildNode(Node node, String childNodeName) {
    final NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        final Node childNode = children.item(i);
        if (childNode.getNodeName().equals(childNodeName)) {
            return childNode;
        }/*from   w  w w.j av  a2 s  .  c o m*/
    }
    return null;
}

From source file:Main.java

public static void copyAttributes(Node from, Node to) {
    NamedNodeMap map = from.getAttributes();
    for (int i = 0; i < map.getLength(); i++) {
        Node attr = map.item(i);
        addAttribute(to, attr.getNodeName(), attr.getNodeValue());
    }//from ww w .ja  v  a2 s .  co  m
}

From source file:Main.java

public static Element get_child_with_attr(Node parent, String child_name, String attr_name, String attr_value) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node child = children.item(i);
        if (child.getNodeName().equals(child_name)) {
            if (child instanceof Element) {
                Element e = (Element) child;
                if (e.getAttribute(attr_name).equals(attr_value))
                    return e;
            }//ww w  . j ava 2  s.c  o  m
        }
    }
    return null;
}

From source file:Main.java

/**
 * Converts {@link NamedNodeMap} to a {@link LinkedHashMap}&lt;String,String&gt;.
 * @param _nodeMap node map//  ww w  . j a  va 2 s. co  m
 * @return {@link LinkedHashMap}, maybe empty but never null
 */
public static Map<String, String> convertToAttributeMap(NamedNodeMap _nodeMap) {
    Map<String, String> map = new LinkedHashMap<>();
    for (int i = 0; i < _nodeMap.getLength(); i++) {
        Node node = _nodeMap.item(i);
        map.put(node.getNodeName(), node.getNodeValue());
    }
    return map;
}

From source file:Main.java

/**
 * Search a child node by type/*from w w  w  . j av  a2s  .  co m*/
 * 
 * @param parent   the parent node
 * @param nodeType  the node type for searching
 * @return         Node with the specified name
 * @see            Node
 * @throws Exception
 */
public static Node findChildNodeByType(Node parent, String nodeType) throws Exception {
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node.getNodeName().equals("Node")) {
            String strType = node.getAttributes().getNamedItem("type").getNodeValue();
            if (strType.equals(nodeType))
                return node;
        }
    }
    return null;
}

From source file:Main.java

public static String getPrefix(org.w3c.dom.Element el, String ns) {
    NamedNodeMap atts = el.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node node = atts.item(i);
        String name = node.getNodeName();
        if (ns.equals(node.getNodeValue())
                && (name != null && (XMLNAMESPACE.equals(name) || name.startsWith(XMLNAMESPACE + ":")))) {
            return node.getPrefix();
        }/*from  w ww .j  a  va 2 s .  c  om*/
    }
    return null;
}

From source file:Main.java

public static void dump(Node node) {
    System.out.println("Node: " + node.getNodeName());
    NamedNodeMap nnm = node.getAttributes();
    if (nnm != null) {
        for (int i = 0; i < nnm.getLength(); i++) {
            Node n = nnm.item(i);
            System.out.println("   " + n.getNodeName() + ":" + n.getNodeValue());
        }//from  w ww. j a va 2  s.c om
    }
}

From source file:Main.java

/**
 * @param child/*from www .ja v  a 2 s  .com*/
 * @return the single child element or null
 * @throws Exception if the child is present multiple times
 */
public static Element getFirstChild(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) {
            ret = (Element) childNode;
            return ret;
        }
    }
    return null;
}

From source file:Main.java

public static Element[] getDirectElements(Element parent, String name) {

    NodeList children = parent.getChildNodes();
    List<Element> result = new LinkedList<>();

    for (int i = 0; i < children.getLength(); i++) {

        Node node = children.item(i);
        if (node.getNodeName().equals(name)) {
            result.add((Element) node);
        }/* ww w .  jav a2s.c  om*/
    }

    return result.toArray(new Element[0]);
}

From source file:Main.java

/**
 * Returns all attributes of the given XML node as a list of name=value strings.
 * Mostly just for debugging./*from  w w  w . j  a v a  2  s . com*/
 * @param node
 * @return
 */
public static List<String> getAttributes(Node node) {
    List<String> result = new ArrayList<String>();
    NamedNodeMap attrs = node.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Node attrNode = attrs.item(i);
        result.add(attrNode.getNodeName() + "=" + attrNode.getNodeValue());
    }
    return result;
}