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 String getNodeValue(Node node) {
    return (node == null || node.getFirstChild() == null || node.getFirstChild().getNodeValue() == null) ? null
            : node.getFirstChild().getNodeValue().trim();
}

From source file:Main.java

/**
 * get the value of an Element in the Xml Document.
 * finds the first occurance of the element and retrieves its value.
 * @param root the root Element.//w  w  w . j a va 2s.  co m
 * @param elemName the name of the element to search for.
 * @return String the element value or null if not found.
 */
public static String getElementValue(Element root, String elemName) {
    NodeList nl = root.getElementsByTagName(elemName);
    if (null == nl) {
        return (null);
    }
    Node n = nl.item(0);
    return (n.getFirstChild().getNodeValue().trim());
}

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   w w w  .  j a va  2  s .c  o m

        childNode = childNode.getNextSibling();
    }

    return true;
}

From source file:Main.java

/**
 * Returns the text associated to the given node
 * i.e. the value of the/*from w  w w .  j a va2 s .c om*/
 * @param node
 * @return
 */
public static String getText(Node node) {
    Node textNode = node.getFirstChild();
    if (textNode == null) {
        return null;
    }
    short nodeType = textNode.getNodeType();
    if (nodeType != Node.TEXT_NODE && nodeType != Node.CDATA_SECTION_NODE) {
        return null;
    }
    return textNode.getNodeValue().trim();
}

From source file:Main.java

/**
 * Getter for a specific text content of the given node.
 * @param node Node.// w w  w.  j a  v  a  2s . c  om
 * @param name Name of element tags around the text content.
 * @return Returns the content or null if no content with the given
 * name exists.
 */
static public String getTextContent(Node node, String name) {
    node = node.getFirstChild();
    while (node != null) {
        if (node.getNodeName().equals(name))
            return (node.getTextContent());
        node = node.getNextSibling();
    }
    return (null);
}

From source file:Main.java

public static void removeChildNodes(Node node) {
    for (Node current = node.getFirstChild(), next; current != null; current = next) {
        next = current.getNextSibling();
        node.removeChild(current);//from  w w  w. j a v  a 2  s . co m
    }
}

From source file:Main.java

public static Element firstChildElement(Node node) {
    for (Node tempNode = node.getFirstChild(); tempNode != null; tempNode = tempNode.getNextSibling()) {
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) tempNode;
        }/*from  w ww . java2s.  c  o m*/
    }
    return null;
}

From source file:Main.java

/**
 * Returns the content of the specified node.
 * <p>//  w w w  . jav  a2 s  .  com
 * Example:
 *
 * <pre>
 * {@code <name>John</name>
 * 
 * getContent(name); // returns "John"}
 * </pre>
 *
 * @param node
 *            The node to get the field child from.
 * @return The content of the specified node, or {@code null} if no such content exists.
 */
public static String getContent(Node node) {
    Node fieldNode = node.getFirstChild();
    if (fieldNode == null) {
        return null;
    }
    return fieldNode.getNodeValue();
}

From source file:Main.java

public static Node findChild(Node node, String chName) {
    for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equals(chName)) {
            return child;
        }/*from   w  w w .j av  a 2 s  .c  o m*/
    }
    return null;
}

From source file:Main.java

/**
 * Returns the value of the first child node of the given node, and asserts
 * that the child node is a Text node./*from   w  w  w  .  j ava 2 s .c  om*/
 * 
 * @param node a node containing a single Text child node
 * @return the value of the child Text node
 */
public static String getChildText(Node node) {
    Node child = node.getFirstChild();
    assert (child.getNodeType() == Node.TEXT_NODE);
    return child.getNodeValue();
}