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

public static void replaceText(Node node, String text) {
    for (;;) {/*from   w  w  w .  j  a v  a 2s  .c o m*/
        Node n = node.getFirstChild();
        if (n == null) {
            break;
        }
        node.removeChild(n);
    }
    Text t = node.getOwnerDocument().createTextNode(text);
    node.appendChild(t);
}

From source file:Main.java

/**
 * The function getFirstChildWithTagName() returns the first child node from
 * a node, identified by its node name.// w  w  w.  j a  va 2 s .  c o  m
 *
 * @param data
 *            Document or Element whose children shall be examined
 * @param tagName
 *            name of the node to find
 * @return first child node with that node name
 * @throws NoSuchElementException
 *             if no child node with that name can be found
 */
public static Element getFirstChildWithTagName(Node data, String tagName) {
    for (Node element = data.getFirstChild(); element != null; element = element.getNextSibling()) {
        if (!(element instanceof Element)) {
            continue;
        }
        if (element.getNodeName().equals(tagName)) {
            return (Element) element;
        }
    }
    throw new NoSuchElementException(tagName);
}

From source file:Main.java

public static Element getFirstElementChild(final Node node) {

    Element elem = null;/*from ww  w.j  a  v  a 2  s  .c o  m*/
    for (Node childNode = node.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) {
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            elem = (Element) childNode;
            return elem;
        }
    }
    return elem;

}

From source file:Main.java

public static String getTextWithoutTrim(Node n) {
    StringBuffer sb = new StringBuffer();
    for (Node ele = n.getFirstChild(); ele != null; ele = ele.getNextSibling()) {
        String name = ele.getNodeName();
        if (name.equalsIgnoreCase("#text"))
            sb.append(ele.getNodeValue());
    }//w  w w  .  j  a va  2  s  .c o  m
    return sb.toString();
}

From source file:Main.java

public static void removeChildNodes(Node parent) {
    while (parent.hasChildNodes()) {
        parent.removeChild(parent.getFirstChild());
    }//from w  w w  .j  a  v a 2s .  c  o m
}

From source file:Main.java

/**
 * Removes text nodes that are only containing whitespace characters
 * inside a DOM tree./*from  w w w . j  a v a  2 s . c  om*/
 *
 * @param element the root node to normalize.
 */
public static void stripWhitespaceNodes(Node element) {
    Node node, child;
    for (child = element.getFirstChild(); child != null; child = node) {
        node = child.getNextSibling();
        stripWhitespaceNodes(child);
    }

    if (element.getNodeType() == Node.TEXT_NODE && element.getNodeValue().trim().length() == 0) {
        element.getParentNode().removeChild(element);
    }
}

From source file:Main.java

/**
 * Remove all children with specified name from the specified node
 *///from  w  w  w  .j  a v  a2s  . co  m
public static void removeChildren(Node node, String name) {
    Node currentChild = node.getFirstChild();

    while (currentChild != null) {
        final Node nextChild = currentChild.getNextSibling();

        if (currentChild.getNodeName().equals(name))
            node.removeChild(currentChild);

        currentChild = nextChild;
    }
}

From source file:Main.java

public static void removeAll(Node node) {
    while (node.getChildNodes().getLength() > 0) {
        node.removeChild(node.getFirstChild());
    }//from  w  w  w.  j  a v  a  2s .  com
}

From source file:Main.java

public static void removeChildren(Node node) {
    while (node.hasChildNodes()) {
        node.removeChild(node.getFirstChild());
    }//w  w w . j  a  v a2  s. c o  m
}

From source file:Main.java

/**
 * Gets the node value//  w ww  . j  a v a 2s.co  m
 * 
 * @param node Node 
 * @return String node value
 */
public static String getNodeValue(Node node) {
    String value = null;
    if (node.getFirstChild() != null) {
        value = node.getFirstChild().getNodeValue();
    }
    return value;
}