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

/**
 *  Given a todo element, we must get the element specified
 *  by the tagName, then must traverse that Node to get the
 *  value.//w w  w  . jav a  2  s . c om
 * @param e
 * @param tagName
 * @return the value of the corresponding element
 */
public static String getValue(Element e, String tagName) {
    try {
        // Get node lists of a tag name from an Element
        NodeList elements = e.getElementsByTagName(tagName);
        Node node = elements.item(0);
        NodeList nodes = node.getChildNodes();

        // Find a value whose value is non-whitespace
        String s;
        for (int i = 0; i < nodes.getLength(); i++) {
            s = ((Node) nodes.item(i)).getNodeValue().trim();
            if (s.equals("") || s.equals("\r")) {
                continue;
            } else {
                return s;
            }
        }
    } catch (Exception ex) {
        return null;
    }
    return null;
}

From source file:Main.java

/**
 * Checks if a node has a child of ELEMENT type.
 * /*from   ww w.  j a va 2 s.c o m*/
 * @param node
 *            a node
 * @return true if the node has a child of ELEMENT type
 */
public static boolean hasElementChild(Node node) {
    NodeList nl = node.getChildNodes();
    Node child = null;
    int length = nl.getLength();
    for (int i = 0; i < length; i++) {
        child = nl.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            return true;
        }
    }

    return false;
}

From source file:Main.java

/***
 * Remove all children nodes/*from  w  w w.j  a v  a  2s.c  om*/
 * 
 * @param node
 *            node to remove children from
 * 
 */
public static void removeAllChilden(final Node node) {
    if (node != null) {
        final NodeList childrens = node.getChildNodes();

        for (int i = 0; i < childrens.getLength(); i++) {
            node.removeChild(childrens.item(i));
        }
    }
}

From source file:Main.java

/**
 * Gets <code>CDATASection</code> element for node.
 *
 * @param node node with text.//w ww  .j  av  a2s  . co m
 * @return text, that contains the specified node.
 */
public static String getNodeCDATASection(final Node node) {
    //        node.normalize();
    final NodeList list = node.getChildNodes();
    for (int i = 0, len = list.getLength(); i < len; i++) {
        final Node child = list.item(i);
        if (child.getNodeType() == Node.CDATA_SECTION_NODE)
            return child.getNodeValue();
    }
    return null;
}

From source file:Main.java

public static List<Node> getValidChildNodes(Node node) {
    List<Node> nl = new ArrayList<>();
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        if (isValidNode(nodeList.item(i)))
            nl.add(nodeList.item(i));/*from   www.  ja  v a 2s.  c  om*/
    }
    return nl;
}

From source file:Main.java

/**
 * getNodeText//from  w w w  .ja v  a2  s  .  c  o m
 * @param n node
 * @return text node content
 */
public static String getNodeText(Node n) {
    NodeList nl = n.getChildNodes();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < nl.getLength(); i++) {
        Node cn = nl.item(i);
        if (cn.getNodeType() == Node.TEXT_NODE) {
            Text txt = (Text) cn;
            sb.append(txt.getData().trim());
        }
    }

    return sb.toString();
}

From source file:Main.java

public static Node findChildNode(Node node, String name) {
    Node child;/*from  ww w. j  a  v a  2  s.  c  om*/
    NodeList list;

    list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        child = list.item(i);
        if (child instanceof Element && child.getNodeName().equals(name)) {
            return child;
        }
    }

    return null;
}

From source file:Main.java

/**
 * Helper Method. Searches through the child nodes of a node and returns the first node with a matching name.
 * Do we need this ?/*ww w .  j  a v  a2s  .  c om*/
 * @param element Element
 * @param name String
 * @param caseSensitive boolean
 * @return Node
 */

public static Node getChildNodeByName(Node element, String name, boolean caseSensitive) {
    NodeList list = element.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (caseSensitive) {
            if (node.getNodeName().equals(name))
                return node;
        } else {
            if (node.getNodeName().equalsIgnoreCase(name))
                return node;
        }
    }
    return null;
}

From source file:Main.java

public static Vector<String> getHandsets(String file) throws Exception {
    Vector<String> vec = new Vector();
    DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder dombuilder = domfac.newDocumentBuilder();
    FileInputStream is = new FileInputStream(file);

    Document doc = dombuilder.parse(is);
    NodeList nodeList = doc.getElementsByTagName("devices");
    if (nodeList != null && nodeList.getLength() >= 1) {
        Node deviceNode = nodeList.item(0);
        NodeList children = deviceNode.getChildNodes();
        if (children != null && children.getLength() >= 1) {
            for (int i = 0; i < children.getLength(); i++) {
                vec.add(children.item(i).getTextContent());
            }/*  w  w  w  . j  ava  2 s  . co m*/
        }
    }
    return vec;
}

From source file:Main.java

private static String getShiftId(Node node) {
    String name = "";
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node curNode = nodeList.item(i);
        if ("shiftId".equalsIgnoreCase(curNode.getNodeName())) {
            name = curNode.getTextContent().replace("\"", "");
            break;
        }//w ww .  j  av a 2 s. co  m
    }

    return name;
}