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

/**
 * only returns the first instance of this child node
 * @param node// www .  j av  a  2 s.  c  o m
 * @param localName
 * @return
 */
public static Node getChildNode(Node node, String name) {
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node temp = nodeList.item(i);
        if (temp.getNodeType() != Node.ELEMENT_NODE)
            continue;
        if (name.equals(temp.getLocalName()) || name.equals(temp.getNodeName()))
            return (temp);
    }

    return (null);
}

From source file:Main.java

public static List<Node> extractChildElements(Node parent) {
    List<Node> result = new ArrayList<Node>();
    NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            result.add(child);//from  ww  w.  j a  va  2s .  co m
        }
    }
    return result;
}

From source file:Main.java

/**
 *  Returns the first child element found with the specified tag name
 *  @param node The node to search/*w  w  w  . ja  v  a2s .  co  m*/
 *  @param element The element tag name to search for
 *  @return The matching element Node
 */
public synchronized static Node getFirstElementWithTagName(Node node, String element) {
    if (node != null && element != null) {
        NodeList nl = node.getChildNodes();

        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);

            if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(element))
                return n;
        }
    }

    return null;
}

From source file:Main.java

public static String readTextNode(Node item) {
    StringBuffer buf = new StringBuffer(""); //$NON-NLS-1$
    NodeList children2 = item.getChildNodes();
    if (children2 != null) {
        for (int k = 0; k < children2.getLength(); k++) {
            Node item2 = children2.item(k);
            if (item2 != null) {
                buf.append(item2.getNodeValue());
            }/*from w  w  w .j  av  a2s  .c om*/
        }
    }
    return buf.toString();
}

From source file:Main.java

public static String getChildNodeText(Node node, String pathname) {
    Node childNode = getChildNode(node, pathname);
    if (childNode != null) {
        Text bb = (Text) childNode.getChildNodes().item(0);
        return bb.getNodeValue();
    }/*  w  ww. ja  v a  2  s . c o m*/
    return "";
}

From source file:Main.java

/**
 *  Returns the first child element found with the specified tag name
 *  @param node The node to search//from  w  w w.  j ava  2 s . c  om
 *  @param element The element tag name to search for
 *  @return The matching element Node
 */
public synchronized static Node getFirstElementIgnoreCase(Node node, String element) {
    if (node != null && element != null) {
        NodeList nl = node.getChildNodes();

        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);

            if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equalsIgnoreCase(element)) {
                return n;
            }
        }
    }

    return null;
}

From source file:Main.java

public static Node getChildWithTagName(final Node node, String name) {
    Node r = null;/*from ww  w.jav  a 2 s  . com*/
    for (Node n : iterate(node.getChildNodes())) {
        if (n.getNodeName().equals(name)) {
            r = n;
            break;
        }
    }
    return r;

}

From source file:DomUtil.java

/**
 * Extract the textual content from a Node.
 * This is rather like the XPath value of a Node.
 * @param node The node to extract the text from
 * @return The textual value of the node
 *//*www .  java  2  s . c om*/
public static String getText(Node node) {
    StringBuffer reply = new StringBuffer();

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

        if ((child instanceof CharacterData && !(child instanceof Comment))
                || child instanceof EntityReference) {
            reply.append(child.getNodeValue());
        } else if (child.getNodeType() == Node.ELEMENT_NODE) {
            reply.append(getText(child));
        }
    }

    return reply.toString();
}

From source file:Main.java

public static List<Node> getChildren(Node node) {
    ArrayList<Node> children = new ArrayList<Node>();
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        children.add(nodeList.item(i));/*from  w  w  w . jav a 2  s .  c om*/
    }
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            children.add(attributes.item(i));
        }
    }
    return children;
}

From source file:Main.java

/**
  Given a person element, must get the element specified
  by the tagName, then must traverse that Node to get the
  value./*from   w  w  w .j  av  a2 s . c  o  m*/
  Step1) get Element of name tagName from e
  Step2) cast element to Node and then traverse it for its
       non-whitespace, cr/lf value.
  Step3) return it!
        
  NOTE: Element is a subclass of Node
        
  @param    e   an Element
  @param    tagName a tag name
  @return   s   the value of a Node
 */
public static String getValue(Element e, String tagName) {
    try {
        //get node lists of a tag name from a 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) {
        System.out.println(ex);
        ex.printStackTrace();
    }

    return null;

}