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:DOMHelper.java

/**
 * Gets the next sibling of a node that is an element.
 * @param node the node/*from   w  w w.j  ava  2  s  .c o m*/
 * @return the next sibling element or {@code null} if none
 * @throws NullPointerException if {@code node} is {@code null}
 */
public static Element getNextSiblingElement(Node node) {
    do {
        node = node.getNextSibling();
    } while (node != null && node.getNodeType() != Node.ELEMENT_NODE);

    return (Element) node;
}

From source file:Main.java

/**
 * Looks through all child elements of the specified root (recursively) and
 * returns the elements that corresponds to all parameters.
 *
 * @param root the Element where the search should begin
 * @param tagName the name of the node we're looking for
 * @param keyAttributeName the name of an attribute that the node has to
 *            have//  w w  w . ja v  a2s  . c  o  m
 * @param keyAttributeValue the value that attribute must have
 * @return list of Elements in the tree under root that match the specified
 *         parameters.
 * @throws NullPointerException if any of the arguments is null.
 */
public static List<Element> locateElements(Element root, String tagName, String keyAttributeName,
        String keyAttributeValue) {
    List<Element> result = new ArrayList<Element>();
    NodeList nodes = root.getChildNodes();
    Node node;
    int len = nodes.getLength();
    for (int i = 0; i < len; i++) {
        node = nodes.item(i);
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        // is this the node we're looking for?
        if (node.getNodeName().equals(tagName)) {
            Element element = (Element) node;
            String attr = element.getAttribute(keyAttributeName);

            if (attr != null && attr.equals(keyAttributeValue))
                result.add(element);
        }

        // look inside.

        List<Element> childs = locateElements((Element) node, tagName, keyAttributeName, keyAttributeValue);

        if (childs != null)
            result.addAll(childs);

    }

    return result;
}

From source file:Main.java

/**
 * Get all child elements//ww  w . j ava2 s . c  o m
 * @param ele parent element
 * @return list of child elements
 */
public static List<Element> getChildElements(Element ele) {
    List<Element> list = new ArrayList<>();
    NodeList childNodes = ele.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        if (childNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
            list.add((Element) childNodes.item(i));
        }
    }
    return list;
}

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}
 *//* w ww  .j  a  v  a 2 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

/**
 * Gets the first child Element of the node, skipping any Text nodes such as whitespace.
 * /*from  w w w.  j  a  v  a  2s  .  c  om*/
 * @param n The parent in which to search for children
 * @return The first child Element of n, or null if none
 */
public static Element getFirstChildElement(Node n) {
    Node child = n.getFirstChild();
    while (child != null && child.getNodeType() != Node.ELEMENT_NODE) {
        child = child.getNextSibling();
    }

    if (child != null) {
        return (Element) child;
    } else {
        return null;
    }
}

From source file:Main.java

private static List<String> findAllBeanIds(Element parent, String tag) {
    List<String> beanIds = new ArrayList<String>();
    // First check to see if any parameters are null
    if (parent == null || tag == null)
        return null;
    // Check to see if this is the element we are interested in. This is
    // redundant apart from first call, but keep in to keep functionality
    //      if (nodeNameEqualTo(parent, tag))
    //         return parent;
    // Get all the children
    NodeList list = parent.getChildNodes();
    int listCount = list.getLength();
    for (int k = 0; k < listCount; k++) {
        Node child = list.item(k);
        // If the node is not an element, ignore it
        if (child.getNodeType() != Node.ELEMENT_NODE)
            continue;
        // Check to see if this node is the node we want
        if (nodeNameEqualTo(child, tag)) {
            beanIds.add(((Element) child).getAttribute("id"));
        }//from w w w .ja v  a 2  s . co m
    }

    return beanIds;
}

From source file:Main.java

protected static Element getChildElement(Element element, String tagName) {
    NodeList childNodes = element.getChildNodes();
    int numChildren = childNodes.getLength();

    for (int i = 0; i < numChildren; i++) {
        Node childNode = childNodes.item(i);
        if (childNode.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }/*w w w .ja v  a 2s. co  m*/
        Element childElement = (Element) childNode;

        if (tagName != null) {
            String childTagName = childElement.getTagName();
            if (!childTagName.equals(tagName)) {
                continue;
            }
        }
        return childElement;
    }
    return null;
}

From source file:Main.java

/**
 * Get child elements by classname/*www  .  j a  v a  2  s .  c o m*/
 *
 * @param ele parent element
 * @param cname of elements to find
 */
public static List<Element> getElementsByClass(Element ele, String cname) {
    Vector<Element> list = new Vector();
    NodeList nl = ele.getChildNodes();
    for (int j = 0; j < nl.getLength(); j++) {
        if (nl.item(j).getNodeType() != Node.ELEMENT_NODE)
            continue;
        Element e = (Element) nl.item(j);
        if (e.getAttribute("class").equals(cname))
            list.add(e);
    }
    return (list);
}

From source file:Main.java

private static Map<?, ?> getNodeBean(Node parent) {
    Map<Object, Object> rtn = new HashMap<Object, Object>();

    if (parent != null) {
        Map<Object, Object> attrMap = new HashMap<Object, Object>();
        if (parent.hasAttributes()) {
            NamedNodeMap attrs = parent.getAttributes();
            for (int j = 0; j < attrs.getLength(); j++) {
                Node attr = attrs.item(j);
                attr.getNodeName();// w w  w . j a  v  a  2s.  c  o m
                attr.getNodeValue();
                attrMap.put(attr.getNodeName(), attr.getNodeValue());
            }
        }

        rtn.put("tagName", parent.getNodeName());
        rtn.put("attr", attrMap);

        NodeList nodeList = parent.getChildNodes();
        if (nodeList != null) {
            List<Object> children = new ArrayList<Object>();
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node child = nodeList.item(i);
                if (child.getNodeType() == Node.ELEMENT_NODE) {
                    children.add(getNodeBean(child));
                }
            }

            rtn.put("children", children);
        }
    }

    return rtn;
}

From source file:Util.java

/**
 * Renvoie les lments enfants (uniquement de type ELEMENT)
 * //from  w w w.ja v a2 s  . com
 * @param e
 * @return
 */
public static Iterable<Element> getChildElements(final Element e) {
    return new Iterable<Element>() {
        public Iterator<Element> iterator() {
            final NodeList list = e.getChildNodes();
            int i = 0;
            for (; i < list.getLength(); i++) {
                if (list.item(i).getNodeType() == Node.ELEMENT_NODE)
                    break;
            }
            final int init = i;
            return new Iterator<Element>() {
                int cur = init;

                public void remove() {
                    throw new UnsupportedOperationException();
                }

                public Element next() {
                    Element item = (Element) list.item(cur);
                    for (cur++; cur < list.getLength(); cur++) {
                        if (list.item(cur).getNodeType() == Node.ELEMENT_NODE)
                            break;
                    }
                    return item;
                }

                public boolean hasNext() {
                    return cur < list.getLength();
                }

            };
        }
    };
}