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

private static void getByName(Node node, String name, List<Node> result) {
    if (node.getNodeName().equalsIgnoreCase(name)) {
        result.add(node);/* ww  w .j  a va 2  s.  c o m*/
    } else {
        NodeList list = node.getChildNodes();
        for (int i = 0, len = list.getLength(); i < len; i++) {
            getByName(list.item(i), name, result);
        }
    }
}

From source file:Main.java

/**
   Return the first children of a node with a given name.
   @return <code>null</code> if no child is found
 *///  www  .j av a 2  s.  c o  m
static public Node findChild(Node node, String child_name) {
    if (node == null)
        return null;

    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeName().equals(child_name))
            return child;
    }
    return null;
}

From source file:Main.java

public static Map<String, String> findAttributesValues(Node node) {

    Map<String, String> retval = new HashMap<String, String>();
    NamedNodeMap attrs = node.getAttributes();
    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            String name = attr.getNodeName();
            String value = attr.getNodeValue();

            retval.put(name, value);/*from   w  ww.j  ava  2s  .c  om*/
        }
    }

    return retval;
}

From source file:Main.java

/**
 * Retrieves a Map of the value of child nodes in the form { [Node Name] =>
 * [Node Value] }// www .  j  a  v a  2s . c  o  m
 * 
 * @param node
 */
public static Map<String, String> getChildValueMap(Node node) {
    HashMap<String, String> map = new HashMap<String, String>();

    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node n = children.item(i);
        String nodeName = n.getNodeName();
        String nodeValue = getNodeValue(n);

        map.put(nodeName, nodeValue);
    }

    return map;
}

From source file:Main.java

/** Remove stray "xmlns" default namespace element that seems to get left over even after removing namespacing from nodes.
 * @param node node to process//from w w w  .  ja  v  a2  s  .  c  o m
 */
public static void removeXmlNsAttribute(final Node node) {
    final NamedNodeMap attr = node.getAttributes();
    for (int i = 0; i < attr.getLength(); i++) {
        final Node item = attr.item(i);
        if (ATTR_XMLNS.equals(item.getNodeName())) {
            attr.removeNamedItem(ATTR_XMLNS);
            return;
        }
    }
}

From source file:Main.java

public static Element getChild(Element element, String fieldName) {
    if ((element == null) || (fieldName == null) || fieldName.equals("")) {
        return null;
    }/*from   w  w w  . j ava 2 s.  c  om*/
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if ((child instanceof Element) && child.getNodeName().equals(fieldName)) {
            return (Element) child;
        }
    }
    return null;
}

From source file:Main.java

public static Node getChildNode(Node parentNode, String childName) {
    NodeList children = parentNode.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node.getNodeName().equalsIgnoreCase(childName))
            return (node);
    }//from   w  ww  . ja v  a 2s.  c  om
    return (null);
}

From source file:Main.java

public static Iterable<Node> getChildrenWithTagName(final Node node, String name) {
    List<Node> list = new ArrayList<>();
    for (Node n : iterate(node.getChildNodes())) {
        if (n.getNodeName().equals(name)) {
            list.add(n);//from w  w  w  . j  av  a 2 s. c  o m
        }
    }
    return list;
}

From source file:Main.java

public static void printNodeList(NodeList nodeList) {
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        buffer.append(node.getNodeName() + " = " + node.getNodeValue() + " Attributes: " + attributesStr(node)
                + "\n");
    }/*from w  w  w. ja  v a  2  s. c o  m*/
    logger.info(buffer);
}

From source file:Main.java

/**
 * * Convenience method to transfer a node (and all of its children) from one
 * * DOM XML document to another./*  w  w w. ja  va2s . c  om*/
 * *
 * * Note: this method is recursive.
 * *
 * * @param current the current Element to append the transfer to
 * * @param target the target document for the transfer
 * * @param n the Node to transfer
 * * @return Element the current element.
 */
public static Element transferNode(Element current, Document target, Node n) {
    String name = n.getNodeName();
    String value = n.getNodeValue();
    short type = n.getNodeType();

    if (type == Node.ELEMENT_NODE) {
        // create a new element for this node in the target document
        Element e = target.createElement(name);

        // move all the attributes over to the target document
        NamedNodeMap attrs = n.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node a = attrs.item(i);
            e.setAttribute(a.getNodeName(), a.getNodeValue());
        }

        // get the children for this node
        NodeList children = n.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            // transfer each of the children to the new document
            transferNode(e, target, children.item(i));
        }

        // append the node to the target document element
        current.appendChild(e);
    } else if (type == Node.TEXT_NODE) {
        Text text = target.createTextNode(value);
        current.appendChild(text);
    }

    return current;
}