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

/**
 * Find node by name.//from w  w w  .j a v a2  s .  co m
 * @param node Node
 * @param nodeName String
 * @return Node
 */
public static Node getChildByName(Node node, String nodeName) {
    org.w3c.dom.NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node c = children.item(i);
        if (c.getNodeName().equals(nodeName))
            return c;
    }
    return null;
}

From source file:Main.java

public static Element getElementByTagName(Node element, String name) {
    NodeList elements = element.getChildNodes();
    if (elements != null) {
        for (int i = 0; i < elements.getLength(); i++) {
            if (elements.item(i).getNodeType() == Node.ELEMENT_NODE) {
                Element currentElement = (Element) elements.item(i);
                if (currentElement.getNodeName().equals(name)) {

                    return currentElement;
                }/*from w ww . j  a  v a2  s. c  om*/
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * Retrieves the value of a child node given the name of the child node.
 * /*w  ww .ja v  a 2 s.  c o  m*/
 * @param node
 * @param childNodeName
 * @return
 */
public static String getChildValue(Node node, String childNodeName) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node n = children.item(i);
        if (n.getNodeName().equals(childNodeName)) {
            return getNodeValue(n);
        }
    }

    return null;
}

From source file:Main.java

/**
 * Gets the first element with the given tag name in the immediate child elements.
 * //www .j  av  a  2  s.  co m
 * @param element
 * @param tagName
 * @return
 */
public static Element getFirstChildElementByTagName(Node element, String tagName) {
    NodeList list = element.getChildNodes();
    int size = list.getLength();
    if (size > 0) {
        for (int i = 0; i < size; i++) {
            Node node = list.item(i);
            if (node instanceof Element) {
                Element e = (Element) node;
                if (e.getTagName().equals(tagName)) {
                    return e;
                }
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Returns the first child of a node with a specified name.
 * // ww  w  .  java2 s .  co m
 * @param node The parent node.
 * @param name The name of the child.
 * @return The first child of <code>node</code> named <code>name</code>.
 */
public static Node getChildByName(Node node, String name) {
    try {
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            if (list.item(i).getNodeName().equals(name)) {
                return list.item(i);
            }
        }
        return null;

    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

/**
 * Method to get the value of "Value" node
 *//*from  w w  w.j ava2  s . c o  m*/
public static String getValueOfValueNode(Node n) {
    NodeList textNodes = n.getChildNodes();
    Node textNode;
    StringBuffer value = new StringBuffer("");
    for (int j = 0; j < textNodes.getLength(); j++) {
        textNode = textNodes.item(j);
        value.append(textNode.getNodeValue());
    }
    return (value.toString().trim());
}

From source file:Main.java

/**
 * Given a node, returns a map where, for each immediate child of this node
 * that is an element named A with a Text node with data B, there is an
 * entry in the map from A to B. If A contains no textual node, A maps to
 * <TT>null</TT>. If the element A appears more than once, the last
 * element encountered is respected.//w w  w . jav  a  2  s .  c  om
 * 
 * @param node
 *            the node to get the map for
 * @return the map from children element names to their textual contents
 */
public static Map<String, String> elementsToText(Node node) {
    NodeList children = node.getChildNodes();
    Map<String, String> e2t = new HashMap<String, String>();
    for (int i = 0; i < children.getLength(); i++) {
        Node c = children.item(i);
        if (c.getNodeType() != Node.ELEMENT_NODE)
            continue;
        String elementName = ((Element) c).getTagName();
        String text = containedText(c);
        e2t.put(elementName, text);
    }
    return e2t;
}

From source file:Main.java

/**
 * //from   www  . j  a v a2  s. c  o m
 * @param n
 * @param name
 * @param sbStrings
 * this function aims at grabbing all title strings under current sourceOfNode. 
 * @return
 */
public static String getNodeStringsByName(Node n, String name, StringBuffer sbStrings) {
    NodeList l = n.getChildNodes();
    if (l == null)
        return sbStrings.toString();

    for (int i = 0; i < l.getLength(); i++) {
        if (l.item(i).getNodeName().equals(name)) {
            sbStrings.append(l.item(i).getTextContent() + " ");
        } else {
            getNodeStringsByName(l.item(i), name, sbStrings);
        }
    }
    return sbStrings.toString();
}

From source file:Main.java

public static List getChildrenElement(Node n) {
    List lst = new ArrayList();
    NodeList nl = n.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node cn = nl.item(i);//from   ww w. j  a v  a2s.  c o m
        if (cn.getNodeType() == Node.ELEMENT_NODE) {
            lst.add(cn);
        }
    }

    return lst;
}

From source file:Main.java

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

    return name;
}