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:Utils.java

/**
 * Get all prefixes defined on this element for the specified namespace.
 * //from   www.ja v  a  2 s  .c  o m
 * @param element
 * @param namespaceUri
 * @param prefixes
 */
public static void getPrefixes(Element element, String namespaceUri, List<String> prefixes) {
    NamedNodeMap atts = element.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node node = atts.item(i);
        String name = node.getNodeName();
        if (namespaceUri.equals(node.getNodeValue())
                && (name != null && (XMLNAMESPACE.equals(name) || name.startsWith(XMLNAMESPACE + ":")))) {
            prefixes.add(node.getPrefix());
        }
    }
}

From source file:Main.java

public static Element findChildElement(Element parent, String name) {
    if (parent == null) {
        return null;
    }/*from w ww  .  j  a  v  a2  s. c  o  m*/
    org.w3c.dom.Node ret = parent.getFirstChild();
    while (ret != null && (!(ret instanceof Element) || !ret.getNodeName().equals(name))) {
        ret = ret.getNextSibling();
    }
    return (Element) ret;
}

From source file:Main.java

public static org.w3c.dom.Node getMatchingChild(org.w3c.dom.Node node, String name) {
    if (node == null) {
        return null;
    }//from   www .j  a v  a  2s  . c o m

    org.w3c.dom.NodeList childList = node.getChildNodes();
    int len = childList.getLength();
    for (int i = 0; i < len; i++) {
        org.w3c.dom.Node curNode = childList.item(i);
        if (name.equals(curNode.getNodeName())) {
            return curNode;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Find all child node(immediate children only) under current node that is an element node and node name is tagName
 * This is  a combination of getChildNodes() and getElementByTagName().
 * Also this helper method returns an iterable node list for convinient use in the foreach statement.
 * Only element node directly under currentNode are returned.(i.e no grandchildren).   
 * The order of the children are maintained (removing non-element node) 
 * @param currentNode//  w  w w .  j  ava  2  s .c o m
 * @param tagName - case sensitive
 * @return list of element nodes equals tagname (case sensitive) 
 */
public static List<Node> getChildElementsByTagName(Node currentNode, String tagName) {
    List<Node> results = new ArrayList<Node>();
    NodeList childNodes = currentNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if ((child.getNodeType() == Node.ELEMENT_NODE) && child.getNodeName().equals(tagName))
            results.add(child);
    }
    return results;
}

From source file:Main.java

static void listNodes(Node node, String indent) {
    String nodeName = node.getNodeName();
    System.out.println(indent + " Node: " + nodeName);
    short type = node.getNodeType();
    System.out.println(indent + " Node Type: " + nodeType(type));
    if (type == TEXT_NODE) {
        System.out.println(indent + " Content is: " + ((Text) node).getWholeText());
    }//from   w  w  w  .  j ava2 s.c  o m

    NodeList list = node.getChildNodes();
    if (list.getLength() > 0) {
        System.out.println(indent + " Child Nodes of " + nodeName + " are:");
        for (int i = 0; i < list.getLength(); i++) {
            listNodes(list.item(i), indent + "  ");
        }
    }
}

From source file:Main.java

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

From source file:Main.java

public static List<String> getChildListValuesByTagName(Element ele, String childEleName) {

    NodeList nl = ele.getChildNodes();
    List<String> childEles = new ArrayList<String>();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element && childEleName.equals(node.getNodeName())
                || childEleName.equals(node.getLocalName())) {
            childEles.add(getTextValue((Element) node));
        }//from  w  w w  . ja va  2 s .  c o m
    }
    return childEles;
}

From source file:Main.java

public static List getChildren(Element parentEl, String name) {
    List children = new ArrayList();
    NodeList l = parentEl.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);
        if (Node.ELEMENT_NODE == n.getNodeType() && name.equals(n.getNodeName())) {
            children.add((Element) n);
        }/*  www.  j  a v  a  2 s . co m*/
    }
    return children;
}

From source file:Main.java

public static Node[] findChildren(Node node, String name) {
    List<Node> list = new ArrayList<Node>();
    NodeList childNodes = node.getChildNodes();
    int count = childNodes.getLength();
    for (int i = 0; i < count; i++) {
        Node child = childNodes.item(i);
        if (child instanceof Element && (name == null || child.getNodeName().equals(name))) {
            list.add(child);/*from w  w  w .  j ava 2 s.co m*/
        }
    }
    return list.toArray(new Node[list.size()]);
}

From source file:Main.java

public static Node getNextHomoSibling(Node aNode) {
    Node nextSibling = aNode;
    while ((nextSibling = nextSibling.getNextSibling()) != null) {
        if (nextSibling.getNodeName().equals(aNode.getNodeName())) {
            return nextSibling;
        }/*from   www  .ja v a 2  s.c om*/
    }
    return null;
}