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 Element findElement(Node node, String tagName) {
    Element result = null;//www. j  av  a  2  s  . c  o m
    NodeList nodeList = node.getChildNodes();
    if (nodeList == null) {
        return result;
    }
    for (int i = 0; i < nodeList.getLength(); ++i) {
        Element element = checkIfElement(nodeList.item(i), tagName);
        if (element != null) {
            result = element;
            break;
        }
    }
    return result;
}

From source file:Main.java

/** Helper method - gets the text value of an element node. The text value
is stored in the child nodes./*from w  w w  .  j av a2s  . c om*/
   @param pNode A org.w3c.dom.Node object.
   @return Element's text.
 */
public static String getNodeText(Node pNode) {
    NodeList pChildren = pNode.getChildNodes();
    int nLength = pChildren.getLength();

    if (0 == nLength)
        return null;

    String strReturn = "";

    for (int i = 0; i < nLength; i++) {
        Node pChild = pChildren.item(i);

        if (pChild instanceof CharacterData)
            strReturn += ((CharacterData) pChild).getData();
    }

    if (0 == strReturn.length())
        return null;

    return strReturn;
}

From source file:Main.java

/**
 * Gets the sub nodes.//from w  w  w .  jav  a 2 s  .co m
 *
 * @param dataNode
 *            the data node
 * @return the sub nodes
 */
public static List<Node> getSubNodes(Node dataNode) {
    List<Node> returnList = new ArrayList<Node>();

    NodeList list = dataNode.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        returnList.add(node);
    }

    return returnList;
}

From source file:Main.java

/**
 * Searches parent node for matching child nodes.
 *
 * @param node     The parent node/*from   ww  w.j  a v  a2 s.  co  m*/
 * @param nodeName The child node's element name
 * @return A list of matching child Nodes
 */
public static Iterator getNodes(Node node, String nodeName) {
    ArrayList nodes = new ArrayList();
    NodeList nl = node.getChildNodes();
    int nll = nl.getLength();
    for (int i = 0; i < nll; i++) {
        Node n = nl.item(i);
        if (n.getNodeName().equals(nodeName)) {
            nodes.add(n);
        }
    }
    return nodes.iterator();
}

From source file:Main.java

/**
 * @param node/*ww  w.jav a 2  s .  c o m*/
 * @param name
 * @return a map with all children nodes with the given name
 * mapped by the value of their first child
 */
public static Map<String, Node> findChildrenMapByFirstChild(Node node, String name) {
    Map<String, Node> ret = new HashMap<String, Node>();
    NodeList nl = node.getChildNodes();
    int len = nl.getLength();
    Node child;
    for (int i = 0; i < len; i++) {
        child = nl.item(i);
        if (name.equals(child.getNodeName())) {
            ret.put(child.getFirstChild().getNodeValue(), child);
        }
    }
    return ret;
}

From source file:Main.java

public static List<Node> getChildrenByTagName(Node node, String element) {
    List<Node> result = new ArrayList<Node>();
    NodeList childNodes = node.getChildNodes();
    for (int j = 0; j < childNodes.getLength(); j++) {
        Node childNode = childNodes.item(j);
        if ("*".equals(element) || element == null || childNode.getNodeName().equals(element)) {
            result.add(childNode);/*from  www .  ja  va2 s.  c om*/
        }
    }
    return result;
}

From source file:Main.java

public static double getArg(final Node node, final String strName, final double dDefaultValue) {
    return getArg(node.getChildNodes(), strName, dDefaultValue);
}

From source file:Main.java

public static Node getChildNode(Node node, String nodeName) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {

        NodeList nodeList = node.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node n = nodeList.item(i);
            if (n.getNodeName().equals(nodeName)) {
                return n;
            }/*from  w  ww . j ava2  s.  c  o m*/
        }
    }

    return null;
}

From source file:Main.java

public static List<Node> getChildNodeList(Node parentNode, String nodeName) {
    List<Node> childNodeList = new ArrayList<>();
    NodeList nodeList = parentNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (((Element) childNode).getNodeName().equals(nodeName)) {
                childNodeList.add(childNode);
            }//from www.  j  a  va2s  .com
        }
    }
    return childNodeList;
}

From source file:Main.java

public static String getNodeText(Node node) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        NodeList childNodes = node.getChildNodes();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < childNodes.getLength(); i++) {
            if (childNodes.item(i).getNodeType() == Node.TEXT_NODE) {
                Node myNode = childNodes.item(i);
                buffer.append(myNode.getNodeValue());
            }// www  .  j a va  2s  . com
        }
        return buffer.toString();
    }
    return "";
}