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

/**
 * Returns the first XML child tag with the specified name.
 * //from   w ww . j av  a2 s.  co m
 * @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.
 */
public static Node getChildNode(Node node, String name) {
    Node child = node.getFirstChild();
    while (child != null && !child.getNodeName().equals(name)) {
        child = child.getNextSibling();
    }
    return child;
}

From source file:Main.java

/**
 * Returns the first direct child element for the given parent node, which
 * matches the given tagName (probably faster than retrieving all children first).
 * @param parent the parent node under which to search for the element
 * @param tagName the tag name of the element to find
 * @return Element - the first found Element or null if no such Element is present
 *//*from   w ww.  j  a v a  2 s  .  c  om*/
public static Element findFirstChildElement(Node parent, String tagName) {
    Node n = parent.getFirstChild();
    do {
        if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().equals(tagName))) {
            return (Element) n;
        }
        // Android Workaround
        try {
            n = n.getNextSibling();
        } catch (IndexOutOfBoundsException e) {
            n = null;
        }
    } while (n != null);
    return null;
}

From source file:Main.java

public static Element getFirstChildElement(Node node) {
    Node child = node.getFirstChild();
    while (child != null && child.getNodeType() != Node.ELEMENT_NODE) {
        child = child.getNextSibling();/*from  w  w  w  .ja va 2 s  .co m*/
    }
    return (Element) child;
}

From source file:Main.java

/**
 * Remove empty CDATA sections from the given node and all of its descendants. Some parsers
 * silently discard empty CDATA sections, and this method may be used to compare the output from
 * parsers that behave differently with respect to empty CDATA sections.
 * /*from www. j av a  2s  . c o m*/
 * @param node
 *            the node to process
 */
public static void removeEmptyCDATASections(Node node) {
    Node child = node.getFirstChild();
    while (child != null) {
        Node next = child.getNextSibling();
        switch (child.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
            if (child.getNodeValue().length() == 0) {
                child.getParentNode().removeChild(child);
            }
            break;
        case Node.ELEMENT_NODE:
            removeEmptyCDATASections(child);
        }
        child = next;
    }
}

From source file:Main.java

public static void removeChildren(Node parent) {
    for (;;) {/* w ww . ja v  a  2 s . c  o  m*/
        Node n = parent.getFirstChild();

        if (n == null) {
            break;
        }

        parent.removeChild(n);
    }
}

From source file:Main.java

private static boolean isEmptyTag(Node p_node) {
    boolean isEmptyTag = (p_node == null) ? true : (p_node.getFirstChild() == null ? true : false);

    return isEmptyTag;
}

From source file:Main.java

/**
 * Gets the first element child./* w  w w  .ja  v  a2  s.  co  m*/
 */
public static Element getFirstElementChild(Node parent) {
    for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) {
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) n;
        }
    }
    return null;
}

From source file:Main.java

/**
 * @param node//  www.j  a  v  a2 s  .  c o  m
 * @return the new string
 */
public static String replaceComma(Node node) {
    if (node.getFirstChild().getTextContent() == null) {
        return "";
    }

    String[] ss = node.getFirstChild().getTextContent().split(",");

    if (ss.length == 1) {
        return ss[0] + " " + ss[0];
    }

    return ss[0] + " " + ss[1];
}

From source file:Main.java

public static Node getFirstChildElement(Node node) {
    Node firstChildElement = null;
    for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() != Node.TEXT_NODE) {
            firstChildElement = child;//from  ww w . j a v  a 2 s. c  om
            break;
        }
    }

    return firstChildElement;
}

From source file:Main.java

/**
 * Extracts a String from a Document consisting entirely of a String.
 * /*  ww w  .ja v a 2  s  . co m*/
 * @return the String
 */
public static String extractString(Node d) {
    if (d == null) {
        return null;
    }
    return d.getFirstChild().getTextContent();
}