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 Node getNodeByName(Node parent, String name) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeName().equals(name)) {
            return child;
        }/*from   w w  w. j av a  2s .c  o m*/
    }
    return null;
}

From source file:Main.java

public static String getNodeValue(Node node) {
    NodeList childNodes = node.getChildNodes();
    for (int x = 0; x < childNodes.getLength(); x++) {
        Node data = childNodes.item(x);
        if (data.getNodeType() == Node.TEXT_NODE)
            return data.getNodeValue();
    }//from   w ww .j av a  2s .com
    return "";
}

From source file:Main.java

public static final String getText(Node node) {
    if (node.hasChildNodes()) {
        NodeList childNodes = node.getChildNodes();
        if (childNodes.getLength() > 0) {
            Node child = childNodes.item(0);
            if ((child.getNodeType() == Node.CDATA_SECTION_NODE) || (child.getNodeType() == Node.TEXT_NODE)) {
                return child.getNodeValue();
            }/*  w  w  w .  j  a  v a 2 s . c o  m*/
        }
    }
    return null;
}

From source file:Main.java

public static Collection getChildElements(Node node) {
    List elements = new ArrayList();
    NodeList nodes = node.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node childNode = nodes.item(i);
        if (childNode instanceof Element) {
            elements.add(childNode);/* www  .  jav a 2s .c o  m*/
        }
    }
    return elements;
}

From source file:Main.java

public static String getPkey(final Node markitBondNode) {
    final NodeList markitBondNodeChildrenList = markitBondNode.getChildNodes();
    final int length = markitBondNodeChildrenList.getLength();
    for (int j = 0; j < length; ++j) {
        final Node childNode = markitBondNodeChildrenList.item(j);
        final String childName = childNode.getNodeName();
        if ("pkey".equals(childName)) {
            final String issuerIdString = childNode.getTextContent();
            return issuerIdString;
        }/*ww  w  .  ja va 2 s .c o  m*/
    }
    return null;
}

From source file:Main.java

/**
 * Get the first child Element of the supplied node that matches a given tag name.
 *
 * @param node The DOM Node.//from ww w  . ja  v a 2 s.  c om
 * @param name The name of the child node to search for.
 * @return The first child element with the matching tag name.
 */
public static Element getFirstChildElementByName(Node node, String name) {
    NodeList children = node.getChildNodes();
    int childCount = children.getLength();

    for (int i = 0; i < childCount; i++) {
        Node child = children.item(i);
        if (child != null && child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName() != null
                && child.getNodeName().equals(name)) {
            return (Element) child;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Gets a child node by the given name.//from w ww. j a  v  a  2 s. co m
 *
 * @param node the node
 * @param name the name of the child node to find and return
 * @return the child node, or <tt>null</tt> if none found
 */
public static Node getChildNodeByTagName(Node node, String name) {
    NodeList children = node.getChildNodes();
    if (children != null && children.getLength() > 0) {
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child instanceof Element) {
                Element element = (Element) child;
                if (name.equals(element.getTagName())) {
                    return element;
                }
            }
        }
    }
    return null;
}

From source file:Main.java

public static Node getNamedChild(Node node, String name) {
    Node retVal = null;//from  w  ww  . ja v a  2  s .c o m

    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeName() == name) {
            retVal = child;
        }
    }

    return retVal;
}

From source file:Main.java

/** Returns the first child element with the given name from an element.
 *
 *  @param node The parent element to be searched
 *  @param name The element name to search for
 *  @return Node representing the element
 *//* w w w.  j  a v  a2s  . c o  m*/
public static Node getSubnodeByName(Node node, String name) {
    Node retVal = null;
    NodeList nl = node.getChildNodes();
    if (nl != null && nl.getLength() != 0) {
        for (int i = 0; i < nl.getLength(); i++)
            if (nl.item(i).getNodeName().equals(name)) {
                retVal = nl.item(i);
                break;
            }
    }
    return retVal;
}

From source file:Main.java

/**
 * Extracts the {@link Element} for the given name from the parent.
 * /*from w w  w. ja  v  a 2s  . c  o  m*/
 * @param parent
 * @param name
 * @return
 */
public static <T extends Node> T findNode(Node parent, Class<T> type, String name) {
    final NodeList childNodes = parent.getChildNodes();

    for (int i = 0; i < childNodes.getLength(); i++) {
        final Node child = childNodes.item(i);
        if (type.isAssignableFrom(child.getClass()) && name.equals(child.getNodeName())) {
            return type.cast(child);
        }
    }
    return null;
}