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 final static Element subElement(Element superEle, String subName) {
    NodeList list = superEle.getChildNodes();
    if (list == null) {
        return null;
    }//from w w  w  .  ja  v a  2 s .c o m
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equals(subName)) {
                return (Element) node;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Searches parent node and returns first found matching node.
 *
 * @param node     The parent node/* w  w  w .j  a  v  a2 s.c o m*/
 * @param nodeName The child node's element name
 * @return The first matching child Node
 */
public static Node getNode(Node node, String nodeName) {
    NodeList ch = node.getChildNodes();
    int l = ch.getLength();
    for (int i = 0; i < l; i++) {
        Node n = ch.item(i);
        if (n.getNodeName().equals(nodeName)) {
            return n;
        }
    }
    return null;
}

From source file:Main.java

static public Node removeChild(Node xmlNode, String name, boolean ignoreCase) {
    Node removedChild = null;//from  w  w  w .ja va  2  s  .co  m

    String key = name;
    if (ignoreCase)
        key = key.toLowerCase();

    NodeList childNodes = xmlNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        String childName = child.getNodeName();
        if (ignoreCase)
            childName = childName.toLowerCase();

        if (childName.equals(key)) {
            removedChild = child;
            xmlNode.removeChild(child);
            break;
        }
    }
    return removedChild;
}

From source file:Main.java

public static void nodeSubDelete(Element root, String table, String queryType) {
    Node find = getTag(root, table);
    Document doc = find.getOwnerDocument();

    NodeList nl = find.getChildNodes();
    int numnodes = nl.getLength();

    System.out.println("length : " + numnodes);

    Node deleteNode = null;//from w w  w  . j  a  v a2  s. c o  m

    for (int i = 0; i < numnodes; i++) {
        Node n = nl.item(i);
        String name = n.getNodeName();

        if (name.equals(queryType)) {
            deleteNode = n;
            System.out.println("Finding Node : " + deleteNode.getNodeName());

            break;
        }
    }

    find.removeChild(deleteNode);

    print(doc);
}

From source file:Main.java

/**
 * Extracts the {@link Element} for the given name from the parent.
 * //w  w  w.  jav a 2s .c o  m
 * @param parent
 * @param name
 * @return
 */
public static <T extends Node> T findNode(Node parent, Class<T> type, String name) {
    final NodeList childNodes = parent.getChildNodes();

    for (int i = 0; i < childNodes.getLength(); i++) {
        final Node child = childNodes.item(i);
        if (type.isAssignableFrom(child.getClass()) && name.equals(child.getNodeName())) {
            return type.cast(child);
        }
    }
    return null;
}

From source file:Main.java

/**
 * Adds or replaces node in parent.//  ww  w . j  a va 2s . c o m
 * @param parent
 * @param node
 * @throws Exception - Node cannot exist more than once,
 * i.e. multiple nodes with the same name cannot exist in parent.
 */
public static void replaceSingleNode(Element parent, final Node node) throws RuntimeException {

    NodeList nodes = parent.getElementsByTagName(node.getNodeName());

    if (nodes.getLength() > 1) {
        throw new RuntimeException(
                "Parent element contains multiple nodes with the name " + node.getNodeName());
    }
    if (nodes.getLength() == 0) {
        parent.appendChild(node);
    } else {
        parent.replaceChild(node, nodes.item(0));
    }
}

From source file:Main.java

static private void getMatchingNodes(Node node, Pattern[] nodePath, int cur, List<Node> res) {
    if (cur < 0 || cur >= nodePath.length)
        return;// w ww. j  a v  a2 s.c om
    boolean last = (cur == nodePath.length - 1);
    Pattern pattern = nodePath[cur];
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node c = children.item(i);
        if (pattern.matcher(c.getNodeName()).matches()) {
            if (last) {
                res.add(c);
            } else {
                getMatchingNodes(c, nodePath, cur + 1, res);
            }
        }
    }
}

From source file:Main.java

/**
 * aims at finding the location of the current node of its kind.
 * the idea is to count how many previous siblings does it have, 
 * @param aNode//from  w  ww  . j a v  a 2s.c o m
 * @return
 */
public static int getLocHomoSibling(Node aNode) {
    int countOfBranch = 0;
    Node preSibling = aNode;
    while ((preSibling = preSibling.getPreviousSibling()) != null) {
        if (preSibling.getNodeName().equals(aNode.getNodeName())) {
            countOfBranch++;
        }
    }
    return countOfBranch;
}

From source file:Main.java

public static List getAllChildElements(Element parent, String name) {
    List elements = null;/*from w  ww .ja  v  a 2s.  com*/
    if (parent != null) {
        elements = Collections.synchronizedList(new ArrayList());
        NodeList childNodes = parent.getChildNodes();
        for (int current = 0; current < childNodes.getLength(); current++) {
            Node node = childNodes.item(current);
            if ((node instanceof Element) && node.getNodeName().equals(name)) {
                elements.add(node);
            }
        }
    }
    return elements;
}

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  ww  w .  ja v a 2s. co  m
            } // end inner loop
        }
    }
    return returnString;
}