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

public static Element[] getChildrenByAttrubte(Element parent, String attrName, String attrValue) {
    NodeList nodes = parent.getChildNodes();
    int len = nodes.getLength();
    Element temp;//  w  w w.  ja  v  a2  s .co  m
    List<Element> list = new ArrayList<Element>(len);
    if (nodes != null && len > 0) {
        for (int i = 0; i < len; i++) {
            if (nodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
                temp = (Element) nodes.item(i);
                if (getAttribute(temp, attrName).equalsIgnoreCase(attrValue))
                    list.add(temp);
            }
        }
    }
    return list.toArray(new Element[list.size()]);
}

From source file:Main.java

/**
 * Gets the parent element./*from www .j  a v a2s . co m*/
 * 
 * @param element
 *            the element
 * @return the parent element
 */
public static Element getParentElement(Element element) {
    if (element == null) {
        return null;
    }
    Element parentElement = null;
    Node parentNode = element.getParentNode();
    while (parentNode != null && parentElement == null) {
        if (parentNode.getNodeType() == Node.ELEMENT_NODE) {
            parentElement = (Element) parentNode;
        }
        if (parentNode.getNodeType() == Node.DOCUMENT_NODE) {
            parentElement = ((Document) parentNode).getDocumentElement();
            if (element.isSameNode(parentElement)) {
                parentElement = null;
            }
        }
        parentNode = parentNode.getParentNode();
    }

    return parentElement;
}

From source file:Main.java

/**
 *  Returns the first child element found with the specified tag name
 *  @param node The node to search//from ww  w  .  jav  a2  s  .  c  om
 *  @param element The element tag name to search for
 *  @return The matching element Node
 */
public synchronized static Node getFirstElementWithTagName(Node node, String element) {
    if (node != null && element != null) {
        NodeList nl = node.getChildNodes();

        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);

            if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(element))
                return n;
        }
    }

    return null;
}

From source file:Main.java

/**
 *  Returns the first child element found with the specified tag name
 *  @param node The node to search/*w  w w  .j a  v  a2 s .c o  m*/
 *  @param element The element tag name to search for
 *  @return The matching element Node
 */
public synchronized static Node getFirstElementIgnoreCase(Node node, String element) {
    if (node != null && element != null) {
        NodeList nl = node.getChildNodes();

        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);

            if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equalsIgnoreCase(element)) {
                return n;
            }
        }
    }

    return null;
}

From source file:Main.java

public static Element getChildElementNodeByName(Node node, String name) {
    if (node == null) {
        return null;
    }/*  ww  w  . j a  v a  2s .  co m*/
    NodeList nodeList = node.getChildNodes();
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node item = nodeList.item(i);
            if (item != null
                    && (node.getNodeType() == Node.ELEMENT_NODE || node.getNodeType() == Node.DOCUMENT_NODE)
                    && name.equalsIgnoreCase(item.getNodeName())) {
                return (Element) item;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Gets the next comment./*from  w  w  w. j  ava2  s .  c  o  m*/
 * 
 * @param element
 *            the element
 * @return the next comment
 */
public static String getNextComment(Node element) {
    while (element.getNextSibling() != null) {
        Node prev = element.getNextSibling();
        if (prev.getNodeType() == Node.COMMENT_NODE) {
            return prev.getTextContent();
        } else if (prev.getNodeType() == Node.TEXT_NODE) {
            return getNextComment(prev);
        } else if (prev.getNodeType() == Node.ELEMENT_NODE) {
            return null;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Get all the direct children elements of an element.
 *
 * @param parent//from w  w  w  .j a v a 2 s  . c  om
 *            The parent element.
 *
 * @return A list of Element's.
 */
public static List<Element> getElements(final Element parent) {
    final LinkedList<Element> list = new LinkedList<Element>();

    Node node = parent.getFirstChild();

    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            list.add((Element) node);
        }

        node = node.getNextSibling();
    }

    return list;
}

From source file:Main.java

/**
 * @param child// www. ja  va 2s.  com
 * @return the single child element or null
 * @throws Exception if the child is present multiple times
 */
public static Element getFirstChild(Element element, String child) throws Exception {
    NodeList nodes = element.getChildNodes();
    Element ret = null;
    for (int i = 0; i < nodes.getLength(); i++) {
        Node childNode = nodes.item(i);
        if (childNode.getNodeName().equalsIgnoreCase(child) && childNode.getNodeType() == Node.ELEMENT_NODE) {
            ret = (Element) childNode;
            return ret;
        }
    }
    return null;
}

From source file:Main.java

public static Map<String, Object> getPropertiesWithValuesFromXML(Node propNode) {
    Map<String, Object> propertiesWithValues = new HashMap<String, Object>();

    NodeList childList = propNode.getChildNodes();
    for (int i = 0; i < childList.getLength(); i++) {
        Node currentNode = childList.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            String nodeName = currentNode.getLocalName();
            String namespace = currentNode.getNamespaceURI();
            // href is a live property which is handled differently
            String fqn = namespace + ":" + nodeName;
            propertiesWithValues.put(fqn, nodeValue(currentNode));

        }//from www  . jav a 2s . c  om
    }
    return propertiesWithValues;
}

From source file:Main.java

/**
 * @param elem//from  w  ww. j  av  a 2  s.com
 * @param childTag
 * @return
 */
public static Element getFirstChild(Element elem, String childTag) {
    if (elem.hasChildNodes()) {
        NodeList list = elem.getElementsByTagName(childTag);
        int count = list.getLength();

        for (int i = 0; i < count; i++) {
            Node node = list.item(i);
            if (node.getParentNode() != elem)
                continue;

            if (node.getNodeType() == Node.ELEMENT_NODE) {
                return (Element) node;
            }
        }
    }

    return null;
}