Example usage for org.w3c.dom Node getChildNodes

List of usage examples for org.w3c.dom Node getChildNodes

Introduction

In this page you can find the example usage for org.w3c.dom Node getChildNodes.

Prototype

public NodeList getChildNodes();

Source Link

Document

A NodeList that contains all children of this node.

Usage

From source file:Main.java

/**
 * @return a list of all child node text contents with this name
 *//* w  w w  .j a v a2  s. c  o m*/
public static ArrayList<String> getChildNodesTextContents(Node node, String name) {
    ArrayList<String> results = new ArrayList<String>();

    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node temp = nodeList.item(i);
        if (temp.getNodeType() != Node.ELEMENT_NODE)
            continue;
        if (name.equals(temp.getLocalName()) || name.equals(temp.getNodeName())) {
            results.add(temp.getTextContent());
        }
    }

    return (results);
}

From source file:Main.java

public static List<Node> getNodesWithName(Node parent, String name, boolean all_of_them) {
    ArrayList<Node> valid = new ArrayList<>();
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); i++)
        if (children.item(i).getNodeType() == Node.ELEMENT_NODE
                && (name == null || children.item(i).getNodeName().equalsIgnoreCase(name))) {
            valid.add(children.item(i));
            if (!all_of_them)
                break;
        }//  w  w  w.j  a v a  2  s . com
    return valid;
}

From source file:Main.java

public static List<String> getGrandSonListValueByTagName(Element element, String parentName, String eleName) {

    NodeList nl = element.getElementsByTagName(parentName);
    if (null == nl) {
        return null;
    }//from w  ww. ja va  2s. c o  m
    Node item = nl.item(0);
    if (null == item) {
        return null;
    }
    NodeList subNodeList = item.getChildNodes();
    List<String> childEles = new ArrayList<String>();
    Node node = null;
    for (int i = 0; i < subNodeList.getLength(); i++) {
        node = subNodeList.item(i);

        if (node != null) {
            if (node instanceof Element && eleName.equals(node.getNodeName())
                    || eleName.equals(node.getLocalName())) {
                childEles.add(getTextValue((Element) node));
            }
        }
    }

    return childEles;
}

From source file:Main.java

public static Node getFirstNamedNode(Node node, String name) {
    if (node.getNodeName().equalsIgnoreCase(name))
        return node;
    else {/*  w w  w. ja v a  2s . c  o  m*/
        NodeList list = node.getChildNodes();
        for (int i = 0, len = list.getLength(); i < len; i++) {
            Node n = getFirstNamedNode(list.item(i), name);
            if (n != null)
                return n;
        }
        return null;
    }
}

From source file:Main.java

private static void formatNode(Document doc, Node parent, Node node) {
    Node txt = doc.createTextNode("\n    ");
    parent.insertBefore(txt, node);/*from  ww w . java  2 s .  c  om*/
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        txt = doc.createTextNode("\n      ");
        node.insertBefore(txt, children.item(i));
        i += 1;
    }
    txt = doc.createTextNode("\n    ");
    node.appendChild(txt);
}

From source file:XmlUtil.java

/**
 * Returns value of single child text node or <code>null</code>.
 *///from   w  ww  .  ja v  a  2s.  c o  m
public static String getChildTextNodeValue(Node node) {
    if (node.getChildNodes().getLength() != 1) {
        return null;
    }
    Node item0 = node.getChildNodes().item(0);
    if (item0.getNodeType() != Node.TEXT_NODE) {
        return null;
    }
    return item0.getNodeValue();
}

From source file:Main.java

/**
 * Get siblings of the same type as element from parent.
 * /*w w  w  .  j ava 2s.  co  m*/
 * @param parent
 *            parent node.
 * @param element
 *            element.
 * @return List of sibling (from element) under parent
 */
public static List<Node> getSiblings(Node parent, Node element) {
    List<Node> result = new ArrayList<Node>();
    NodeList list = parent.getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {
        Node el = list.item(i);

        if (el.getNodeName().equals(element.getNodeName())) {
            result.add(el);
        }
    }

    return result;
}

From source file:Main.java

public static org.w3c.dom.Node getMatchingChild(org.w3c.dom.Node node, String name) {
    if (node == null) {
        return null;
    }/*  w w w  .  j  a  va  2  s  . 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

/***********************************************************************/
private static Node traverseToInnerTag(String tag, Node node) {
    String name = node.getNodeName();
    if (name.equals(tag)) {
        return node;
    } else {//from w  w w .jav a 2  s  .c o m
        NodeList childNodes = node.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = traverseToInnerTag(tag, childNodes.item(i));
            if (child != null) {
                return child;
            }
        }
    }
    return null;
}

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;/*  w w w.  j  a  va2s .  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);
}