Example usage for org.w3c.dom Node getNextSibling

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

Introduction

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

Prototype

public Node getNextSibling();

Source Link

Document

The node immediately following this node.

Usage

From source file:Main.java

/**
 * This methods extracts the text for a named element under the
 * <code>root</code>. For the moment it uses only the first text node,
 * and only the first element of that type.
 *
 * @param root    The element is contained within this root.
 * @param tagname The elemet name to search for
 * @return The text under the tag, or <code>null</code> if it is not found
 *         or an error occured./*w ww.ja  va  2s  .  c om*/
 */
public static String getElementText(Element root, String tagname) {
    NodeList nameList = root.getElementsByTagName(tagname);

    if (nameList.getLength() > 0) {
        Node text = nameList.item(0).getFirstChild();

        // Find the first text node
        while ((text != null) && !(text instanceof Text)) {
            text = text.getNextSibling();
        }

        if (text == null) {
            return null;
        } else {
            try {
                return text.getNodeValue();
            } catch (DOMException de) {
                return null;
            }
        }
    } else {
        return null;
    }
}

From source file:Main.java

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

From source file:Main.java

private static void removeEmptyTextNodes(Node parentNode) {
    Node childNode = parentNode.getFirstChild();
    while (childNode != null) {
        // grab the "nextSibling" before the child node is removed
        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   w w w .j ava  2  s . com
        }
        childNode = nextChild;
    }
}

From source file:Main.java

/**
 * Gets a TEXT node value from the specified Element node.
 *
 * @param element a ELEMENT Node has TEXT node 
 *//*w w w . ja va2  s.c om*/
/* TODO: Child? */
public static String getTextData(Node element) {
    if (element.getNodeType() != Node.ELEMENT_NODE) {
        throw new IllegalArgumentException(element + " is not ELEMENT node");
    }
    Node description = element.getNextSibling();
    if (description.getNodeType() != Node.TEXT_NODE) {
        throw new IllegalArgumentException(description + " is not TEXT node");
    }
    return description.getNodeValue();
}

From source file:Main.java

/** Finds and returns the first child element node. */
public static Element getFirstChildElement(Node parent) {

    // search for node
    Node child = parent.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) child;
        }//www  . ja v a 2  s.co  m
        child = child.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/** Look for Element node of given name.
 *
 *  <p>Checks the node itself and its siblings for an {@link Element}.
 *  Does not descent down the 'child' links.
 *
 *  @param node Node where to start./* w w w. j a v  a2s. co  m*/
 *  @param name Name of the node to look for.
 *  @return Returns node, the next matching sibling, or <code>null</code>.
 */
private static final Element findElementByName(Node node, final String name) {
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name))
            return (Element) node;
        node = node.getNextSibling();
    }
    return null;
}

From source file:Main.java

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

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

From source file:Main.java

/**
 * Remove any whitespace text nodes from the DOM. Calling this before saving
 * a formatted document will fix the formatting indentation of elements
 * loaded from a different document.//w w  w.  j a v  a  2 s . co m
 * 
 * @param node
 *            Node to remove whitespace nodes from
 * @param deep
 *            Should this method recurse into the node's children?
 */
public static void removeWhitespace(Node node, boolean deep) {
    NodeList children = node.getChildNodes();
    int length = children.getLength();
    for (int i = 0; i < length; i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE && length > 1) {
            Node previous = child.getPreviousSibling();
            Node next = child.getNextSibling();
            if ((previous == null || previous.getNodeType() == Node.ELEMENT_NODE
                    || previous.getNodeType() == Node.COMMENT_NODE)
                    && (next == null || next.getNodeType() == Node.ELEMENT_NODE
                            || next.getNodeType() == Node.COMMENT_NODE)) {
                String content = child.getTextContent();
                if (content.matches("\\s*")) //$NON-NLS-1$
                {
                    node.removeChild(child);
                    i--;
                    length--;
                }
            }
        } else if (deep && child.getNodeType() == Node.ELEMENT_NODE) {
            removeWhitespace(child, deep);
        }
    }
}

From source file:Main.java

public static Node findSubElement(Node parent, String localName) {
    if (parent == null) {
        return null;
    }// ww w .  j a va  2 s  .c o m
    Node child = parent.getFirstChild();
    while (child != null) {
        if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getLocalName().equals(localName))) {
            return child;
        }
        child = child.getNextSibling();
    }
    return null;
}

From source file:Main.java

/**
 * Search a child node by name //from   w w  w .j  a v a2 s  .  c o  m
 * 
 * @param parent   the parent node
 * @param nodeName   the node name for searching
 * @return         Node with the specified name
 * @see            Node
 * @throws Exception
 */
public static Node findChildNodeByName(Node parent, String nodeName) throws Exception {

    Node child = null;
    Node node = parent.getFirstChild();
    while (node != null) {
        if (node.getNodeName().equals(nodeName)) {
            child = node;
            break;
        }
        node = node.getNextSibling();
    }

    return child;
}