Example usage for org.w3c.dom Node ELEMENT_NODE

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

Introduction

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

Prototype

short ELEMENT_NODE

To view the source code for org.w3c.dom Node ELEMENT_NODE.

Click Source Link

Document

The node is an Element.

Usage

From source file:Main.java

/**
 * Returns an iterator over the children of the given element with
 * the given tag name./* w w  w .  jav  a 2 s  .com*/
 * @param element    The parent element
 * @param tagName    The name of the desired child
 * @return           An interator of children or null if element is null.
 */
public static Iterator<Element> getChildrenByTagName(Element element, String tagName) {
    if (element == null)
        return null;
    // getElementsByTagName gives the corresponding elements in the whole 
    // descendance. We want only children
    NodeList children = element.getChildNodes();
    ArrayList<Element> goodChildren = new ArrayList<>();
    for (int i = 0; i < children.getLength(); i++) {
        Node currentChild = children.item(i);
        if (currentChild.getNodeType() == Node.ELEMENT_NODE
                && ((Element) currentChild).getTagName().equals(tagName)) {
            goodChildren.add((Element) currentChild);
        }
    }
    return goodChildren.iterator();
}

From source file:Main.java

public static final String getComment(Element elem) {
    StringBuilder sb = new StringBuilder();
    Node node = elem.getPreviousSibling();
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            break;
        }/*from   w  w  w .  j  a va 2 s . co  m*/
        if (node.getNodeType() == Node.COMMENT_NODE) {
            if (sb.length() > 0) {
                sb.insert(0, '\n');
                sb.insert(0, ((Comment) node).getData());
            } else {
                sb.append(((Comment) node).getData());
            }
        }
        node = node.getPreviousSibling();
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Returns the next Element node.// w ww .  j ava2s .  com
 */
private static Element getNextElementNode(Node node) {
    while (node != null) {
        if (Node.ELEMENT_NODE == node.getNodeType())
            return (Element) node;
        node = node.getNextSibling();
    }
    return null;
}

From source file:Main.java

public static Element[] getChildrenByName(Element parentElement, String childrenName) {
    NodeList nl = parentElement.getChildNodes();
    int max = nl.getLength();
    LinkedList<Node> list = new LinkedList<Node>();
    for (int i = 0; i < max; i++) {
        Node n = nl.item(i);//from ww  w  .j  a  va2 s .  c  om
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(childrenName)) {
            list.add(n);
        }
    }
    return list.toArray(new Element[list.size()]);
}

From source file:Main.java

/**
 * Access all immediate child elements inside the given Element
 *
 * @param element the starting element, cannot be null.
 * @param elemName the name of the child element to look for, cannot be
 * null.//w ww .  j a  va2s  .c o  m
 * @return array of all immediate child element inside element, or
 * an array of size zero if no child elements are found.
 */
public static Element[] getChildElements(Element element, String elemName) {
    NodeList list = element.getChildNodes();
    int len = list.getLength();
    ArrayList<Node> array = new ArrayList<Node>(len);

    for (int i = 0; i < len; i++) {
        Node n = list.item(i);

        if (n.getNodeType() == Node.ELEMENT_NODE) {
            if (elemName.equals(n.getNodeName())) {
                array.add(n);
            }
        }
    }
    Element[] elems = new Element[array.size()];

    return (Element[]) array.toArray(elems);
}

From source file:Main.java

/**
 * Get all child elements with the provided name that are direct children
 * the provided element.//from  w  w w. jav a 2  s  . c o m
 * 
 * @param parent
 *            The parent element
 * @param name
 *            The name of the child elements to find
 * @return The list with child elements, empty list if no matching children
 */
public static Collection<Element> getChildElementsByName(Element parent, String name) {
    assertNotNull(parent);
    Collection<Element> childList = new ArrayList<Element>();

    NodeList children = parent.getChildNodes();
    Node node;
    for (int i = 0; i < children.getLength(); i++) {
        node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) {
            childList.add((Element) node);
        }
    }

    return childList;
}

From source file:Main.java

/** Look for Element node.
 *
 *  <p>Checks the node and its siblings.
 *  Does not descent down the 'child' links.
 *
 *  @param node Node where to start.//from ww  w. j  av a  2 s. com
 *  @return Returns node, next Element sibling or <code>null</code>.
 */
public static final Element findElement(Node node) {
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE)
            return (Element) node;
        node = node.getNextSibling();
    }
    return null;
}

From source file:Main.java

/**
 * Returns a List of all descendant Element nodes having the specified
 * [namespace name] property. The elements are listed in document order.
 *
 * @param node The node to search from./*from   ww  w.j  av  a2s  . c om*/
 * @param namespaceURI An absolute URI denoting a namespace name.
 * @return A List containing elements in the specified namespace; the list
 * is empty if there are no elements in the namespace.
 */
public static List<Element> getElementsByNamespaceURI(Node node, String namespaceURI) {
    List<Element> list = new ArrayList<Element>();
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        if (child.getNamespaceURI().equals(namespaceURI)) {
            list.add((Element) child);
        }
    }
    return list;
}

From source file:Main.java

public static void getInnerText(StringBuilder sb, Element elt, String separator) {
    NodeList tns = elt.getChildNodes();
    for (int j = 0; j < tns.getLength(); j++) {
        Node tn = tns.item(j);/*www .j  a  v  a 2s .  c  o  m*/
        if (tn.getNodeType() == Node.TEXT_NODE) {
            sb.append(tn.getNodeValue());
        } else if (tn.getNodeType() == Node.ELEMENT_NODE) {
            sb.append(separator);
            getInnerText(sb, (Element) tn, separator);
            sb.append(separator);
        }
    }
}

From source file:Main.java

public static Element getLastChild(Element parent, String name) {
    Element child = null;//from w w w.  j a  va2  s.c  o m
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
            child = (Element) n;
        }
    }
    return child;
}