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

public static final Node getSubNodeById(Node n, String id) {
    int i;//from w ww.j a  v  a 2 s .co  m
    NodeList children;
    Node childnode;

    if (n == null) {
        return null;
    }

    children = n.getChildNodes();

    for (i = 0; i < children.getLength(); i++) {
        childnode = children.item(i);
        NamedNodeMap nnm = childnode.getAttributes();
        if (nnm != null) {
            Node attr = nnm.getNamedItem("id");
            if (attr != null && id.equals(attr.getNodeValue())) {
                return childnode;
            }
        }

        childnode = getSubNodeById(childnode, id);
        if (childnode != null) {
            return childnode;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Gets a list of nodes that matches the given attribute name and attribute value
 * @param attrName the given attribute name
 * @param attrValue the given attribute value
 * @param node the node under which search must be performed
 * @return List of nodes that matches the given attribute properties
 *///from   ww w .j a  va2s.co m
public static List<Node> getNodesByAttributeValue(Node node, String attrName, String attrValue) {
    List<Node> list = new ArrayList<Node>();

    NodeList nodeList = node.getChildNodes();
    if (nodeList != null) {
        Node child;
        for (int i = 0; i < nodeList.getLength(); i++) {
            child = nodeList.item(i);
            if (nodeAttributeMatches(child, attrName, attrValue)) {
                list.add(child);
            }
        }
    }

    return list;
}

From source file:Main.java

/**
 * Sets the contents of the child elements that match the type
 * @param parent The element whose children will be updated
 * @param type The name of the children elements to updated
 * @param firstOnly True if only the first child of type is to be updated
 */// www .j a  v a  2 s .  c  om
public static void setChildrenContent(final Node parent, final String type, final String content,
        final boolean firstOnly) {
    final NodeList children = parent.getChildNodes();
    for (int childIndex = 0; childIndex < children.getLength(); ++childIndex) {
        final Node child = children.item(childIndex);
        if (child.getNodeName().equals(type)) {
            child.setTextContent(content);
            if (firstOnly) {
                break;
            }
        }
    }
}

From source file:Main.java

public static void removeAll(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 .ja v  a2s. co m
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            removeAll(list.item(i), nodeType, name);
        }
    }
}

From source file:Main.java

public static String FindNode(NodeList listNode, ArrayList<String> arrStrCompare, String strData) {
    ArrayList<String> arrTempList = arrStrCompare;
    Iterator<String> iterator = arrTempList.iterator();

    while (iterator.hasNext()) {
        String strCompare = (String) iterator.next();
        iterator.remove();//from   w w w.jav  a2s  . c  om

        for (int i = 0; i < listNode.getLength(); i++) {
            Node node = listNode.item(i);

            if (strCompare.equals(node.getNodeName())) {
                if (iterator.hasNext()) {
                    return FindNode(node.getChildNodes(), arrTempList, strData);
                }
                String strResp = node.getTextContent();
                System.out.println("Found NODE [" + strCompare + "]: " + strResp);
                return strResp;
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * Helper Method. Searches through the child nodes of an element and returns an array of the matching nodes.
 * @param element Element//from  www  .  j a va2  s  .  com
 * @param name String
 * @param caseSensitive boolean
 * @return ArrayList
 */
public static List<Node> getChildNodesByName(Node element, String name, boolean caseSensitive) {
    ArrayList<Node> nodes = new ArrayList<Node>();
    NodeList list = element.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (caseSensitive) {
            if (node.getNodeName().equals(name))
                nodes.add(node);
        } else {
            if (node.getNodeName().equalsIgnoreCase(name))
                nodes.add(node);
        }
    }
    return nodes;
}

From source file:Main.java

/**
 * Searches parent node for matching child nodes and collects and returns
 * their matching attribute String values.
 *
 * @param node     The parent node/*from   www  .  jav a 2 s  .  c o  m*/
 * @param elemName The matching child node element name
 * @param attr     The matching child node attribute name
 * @return List of attribute values
 */
public static Enumeration getChildrenAttributeValues(Node node, String elemName, String attr) {
    Vector vect = new Vector();
    NodeList nl = node.getChildNodes();
    int n = nl.getLength();
    for (int i = 0; i < n; i++) {
        Node nd = nl.item(i);
        if (nd.getNodeName().equals(elemName)) {
            vect.add(getAttribute(nd, attr));
        }
    }
    return vect.elements();
}

From source file:Main.java

public static Node SearchNode(NodeList listNode, ArrayList<String> arrStrCompare) {
    ArrayList<String> arrTempList = arrStrCompare;
    Iterator<String> iterator = arrTempList.iterator();

    while (iterator.hasNext()) {
        String strCompare = (String) iterator.next();
        iterator.remove();//w  w  w  .  j  a  v a2 s. c  o  m

        for (int i = 0; i < listNode.getLength(); i++) {
            Node node = listNode.item(i);

            if (strCompare.equals(node.getNodeName())) {
                if (iterator.hasNext()) {
                    return SearchNode(node.getChildNodes(), arrTempList);
                }
                String strResp = node.getTextContent();
                System.out.println("Found DATA [" + strCompare + "]: " + strResp);
                return node;
            }
        }
    }

    return null;
}

From source file:Main.java

public static List<Node> getNodesWithKey(Node parent, String key, Set<String> values, boolean all_of_them) {
    ArrayList<Node> valid = new ArrayList<>();
    NodeList children = parent.getChildNodes();
    Node current;//from  ww w .  java2  s  . com
    for (int i = 0; i < children.getLength(); i++) {
        current = children.item(i);
        NamedNodeMap attrs = current.getAttributes();
        if (attrs != null) {
            Node keynode = attrs.getNamedItem(key);
            if (keynode != null)
                if (values == null || values.contains(keynode.getNodeValue())) {
                    valid.add(current);
                    if (!all_of_them)
                        break;
                }
        }
    }
    return valid;
}

From source file:Main.java

public static String getRequestNameFull(String xml)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));

    NodeList nodeList = document.getDocumentElement().getChildNodes();
    Node reqName = document.getElementsByTagName("request-name").item(0);
    System.out.println(reqName.getTextContent());

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        System.out.println(i + "--" + node);
        if (node instanceof Element) {
            NodeList childNodes = node.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node cNode = childNodes.item(j);
                System.out.println(i + "--" + j + "--" + cNode);
                if (cNode instanceof Element) {
                    String content = cNode.getLastChild().getTextContent().trim();
                    System.out.println(i + "--" + j + "--" + content);
                }/*  w  w  w.ja  va 2s .co  m*/
            }
        }
    }
    /*
     * Do the parsing for reqname
     */
    return reqName.getTextContent();
}