Example usage for org.w3c.dom Node getFirstChild

List of usage examples for org.w3c.dom Node getFirstChild

Introduction

In this page you can find the example usage for org.w3c.dom Node getFirstChild.

Prototype

public Node getFirstChild();

Source Link

Document

The first child of this node.

Usage

From source file:Main.java

/**
 * Sets the text value of an Element. if current text of the element is
 * replaced with the new text if text is null any
 * current text value is deleted./*from  w w w.  j av  a 2 s.c om*/
 * 
 * @param elem the Element for which the text value should be set
 * @param text the new text value of the element
 * @return true if the text could be set or false otherwise
 */
static public boolean setElementText(Node elem, Object text) {
    if (elem == null)
        return false; // Fehler
    // Find Text
    Node node = elem.getFirstChild();
    while (node != null) { // Find all Text nodes
        if (node.getNodeType() == Node.TEXT_NODE)
            break; // gefunden
        node = node.getNextSibling();
    }
    if (node != null) { // Set or remove text
        if (text != null)
            node.setNodeValue(text.toString());
        else
            elem.removeChild(node);
    } else if (text != null) { // Add Text
        elem.appendChild(elem.getOwnerDocument().createTextNode(text.toString()));
    }
    return true;
}

From source file:Main.java

/**
 *  Returns first node at the bottom of path from node.
 * If element begins with '@', indicates an attribute, eg "@id"
 * The '#text' element indicates that the node has a single text child.
 * @param node    Node to apply path to/*from  ww w. java 2 s .c om*/
 * @param path    Path to apply
 * @return        Node at bottom of path, or null
 */
static public Node extractNode(Node node, String path) {
    if (node == null)
        return null;
    NodeList list = node.getChildNodes();
    if (path.equals("#text"))
        return node.getFirstChild();
    else if (path.charAt(0) == '@')
        return node.getAttributes().getNamedItem(path.substring(1));
    else
        for (int j = 0; j < list.getLength(); j++)
            if (list.item(j).getNodeType() == Node.ELEMENT_NODE && list.item(j).getNodeName().equals(path))
                return list.item(j);

    return null;
}

From source file:Main.java

/**
 * Static helper function to get the element data of the specified node.
 * /*ww w .  java  2s  . c om*/
 * @param node
 *            the node where the text data resides; may be <code>null</code>
 *            in which case this funtion will return ""
 * 
 * @return the complete text of the specified node, or an empty string if
 *         the node has no text or is <code>null</code>
 */
public static String getElementData(final Node node) {
    StringBuffer ret = new StringBuffer();

    if (node != null) {
        Node text;
        for (text = node.getFirstChild(); text != null; text = text.getNextSibling()) {
            /**
             * the item's value is in one or more text nodes which are its
             * immediate children
             */
            if (text.getNodeType() == Node.TEXT_NODE || text.getNodeType() == Node.CDATA_SECTION_NODE) {
                ret.append(text.getNodeValue());
            } else {
                if (text.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
                    ret.append(getElementData(text));
                }
            }
        }
    }
    return ret.toString();
}

From source file:Main.java

public static String textContent(Node node) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        StringBuffer sb = new StringBuffer();
        Node nextChild = node.getFirstChild();
        while (nextChild != null) {
            sb.append(textContent(nextChild));
            nextChild = nextChild.getNextSibling();
        }//from www  .j a  v  a2 s . c  o  m
        return sb.toString();
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        return node.getNodeValue();
    default:
        return "";
    }
}

From source file:Main.java

/** Finds and returns the first child node with the given name. */
public static Element getFirstChildElement(Node parent, String elemName) {

    if (parent == null)
        return null;
    // search for node
    Node child = parent.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            if (child.getNodeName().equals(elemName)) {
                return (Element) child;
            }//  ww w.j a  va2  s .c o  m
        }
        child = child.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

static public int findAllDescendantElements(Node e, String qname, Collection vector) {
    int n = 0;//www .  j a  v  a  2 s . c  o  m
    if (e == null || qname == null)
        return 0;
    for (Node c = e.getFirstChild(); c != null; c = c.getNextSibling()) {
        if (c.getNodeType() == Node.ELEMENT_NODE && c.getNodeName().equals(qname)) {
            ++n;
            vector.add((Element) c);
        }
        n += findAllDescendantElements(c, qname, vector);
    }
    return n;
}

From source file:Main.java

/**
 * Searches parent node for matching child nodes and collects and returns
 * their values.//  ww w  .j  a va2  s . c  o  m
 *
 * @param node     The parent node
 * @param elemName The matching child node element name
 * @return List of node values
 */
public static Enumeration getChildrenNodeValues(Node node, String elemName) {
    Vector vect = new Vector();
    NodeList nl = node.getChildNodes();
    int n = nl.getLength();
    for (int i = 0; i < n; i++) {
        Node nd = nl.item(i);
        if (nd.getNodeName().equals(elemName)) {
            Node child = nd.getFirstChild();
            if (child == null) {
                vect.add("");
            } else {
                vect.add(child.getNodeValue());
            }
        }
    }
    return vect.elements();
}

From source file:Main.java

private static void extractText(Node n, StringBuffer buf) {
    if (n.getNodeType() == Node.TEXT_NODE) {
        buf.append(n.getNodeValue());/*ww w. j  av a2s.c  o m*/
    }

    for (n = n.getFirstChild(); n != null; n = n.getNextSibling()) {
        extractText(n, buf);
    }
}

From source file:Main.java

/**
 * Dumps a debug listing of the child nodes of the given node to
 * System.out.//from   w ww  .  ja v  a2 s  .  c o m
 * 
 * @param node the node to dump the children of
 */
public static void dumpChildren(Node node) {
    System.out.println("Children of " + node.getNodeName() + ", NS: " + node.getNamespaceURI() + ", Type: "
            + node.getClass());
    Node child = node.getFirstChild();
    while (child != null) {
        short nodeType = child.getNodeType();
        String nodeName = child.getNodeName();
        String nodeValue = child.getNodeValue();
        String nsURI = child.getNamespaceURI();
        System.out.println("  Type: " + nodeType + ", Name: " + nodeName + ", Value: " + nodeValue + ", NS: "
                + nsURI + ", Type: " + node.getClass());
        child = child.getNextSibling();
    }
}

From source file:Main.java

public static String getChildNodeValue(String nodeName, Node parent) {

    if (parent == null) {
        return null;
    }//  w w w  . j  a v a2 s  . c o m

    NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equals(nodeName)) {
                Node firstChild = node.getFirstChild();
                if ((firstChild != null) && (firstChild.getNodeType() == Node.TEXT_NODE)) {
                    return firstChild.getNodeValue();
                } else {
                    return null;
                }
            }
        }
    }

    return null;
}