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 Node getChildNodeOf(Node node, String tagName) {
    for (Node temp = node.getFirstChild(); temp != null; temp = temp.getNextSibling())
        if (temp.getNodeType() == Node.ELEMENT_NODE && tagName.equals(temp.getNodeName())) {
            return temp;
        }//  ww w  . ja  va  2  s.  c  o  m
    return null;
}

From source file:Utils.java

/**
 * Get the first child of the specified type.
 * //from www. j ava2  s  .com
 * @param parent
 * @param type
 * @return
 */
public static Node getChild(Node parent, int type) {
    Node n = parent.getFirstChild();
    while (n != null && type != n.getNodeType()) {
        n = n.getNextSibling();
    }
    if (n == null) {
        return null;
    }
    return n;
}

From source file:Main.java

public static String getNodeValue(org.w3c.dom.Node node) {
    return node.getFirstChild().getNodeValue();
}

From source file:DOMHelper.java

/**
 * Gets the first child element of a node.
 * @param node the node to get the child from
 * @return the first element child of {@code node} or {@code null} if none
 * @throws NullPointerException if {@code node} is {@code null}
 *//*from w ww  .  j  a va2  s . c om*/
public static Element getFirstChildElement(Node node) {
    node = node.getFirstChild();
    while (node != null && node.getNodeType() != Node.ELEMENT_NODE) {
        node = node.getNextSibling();
    }
    return (Element) node;
}

From source file:Main.java

public static String getTextTag(Node node) {
    Node child = node.getFirstChild();
    if (child.getNodeType() == Node.TEXT_NODE)
        return child.getNodeValue();
    return null;// w  w  w  .  ja v a 2s.  c  o  m
}

From source file:Main.java

/**
 * @param n Node to look under//from w w w. j  a  v  a2s. c o  m
 * @return true if the given node contains any #text children
 */
public static boolean isTextNode(Node n) {
    for (Node ele = n.getFirstChild(); ele != null; ele = ele.getNextSibling()) {
        String name = ele.getNodeName();
        if (!name.equalsIgnoreCase("#text")) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

public static boolean hasOnlyTextChildren(@Nonnull Node node) {
    Node childNode = node.getFirstChild();

    while (childNode != null) {
        if (!(childNode instanceof Text)) {
            return false;
        }//from  www  .  j a  va2s  .  com

        childNode = childNode.getNextSibling();
    }

    return true;
}

From source file:Main.java

/**
 * Returns the first XML child tag with the specified name.
 *
 * @param node The node to search children of
 * @param name The name of the node to search for, case-sensitive.
 * @return The child with the specified name, or null if none exists.
 *///from   w ww .  ja  va  2s .c om
public static Node getChildNode(Node node, String name) {
    Node child = node.getFirstChild();
    while (child != null) {
        if (child.getNodeName().equals(name)) {
            return child;
        }
        child = child.getNextSibling();
    }
    return null;
}

From source file:Main.java

private static void removeEmptyTextNodes(Node parentNode) {
    Node childNode = parentNode.getFirstChild();
    while (childNode != null) {
        Node nextChild = childNode.getNextSibling();
        short nodeType = childNode.getNodeType();
        if (nodeType == Node.TEXT_NODE) {
            boolean containsOnlyWhitespace = childNode.getNodeValue().trim().isEmpty();
            if (containsOnlyWhitespace) {
                parentNode.removeChild(childNode);
            }/*from  www.ja va 2  s  . co  m*/
        }
        childNode = nextChild;
    }
}

From source file:Main.java

/**
 * Removes all children of the specified node.
 * //from   w  ww .  j a  v a2 s . c  om
 * @param node
 *            the node.
 */
public static void removeAllChildren(Node node) {
    Node child;
    while ((child = node.getFirstChild()) != null) {
        node.removeChild(child);
    }
}