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

/**
 * Looks through all child elements of the specified root (recursively)
 * and returns the first element 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/*from  w w  w  . jav  a  2s.  c om*/
 * @param keyAttributeValue the value that attribute must have
 * @return the Element in the tree under root that matches the specified
 * parameters.
 * @throws NullPointerException if any of the arguments is null.
 */
public static Element locateElement(Element root, String tagName, String keyAttributeName,
        String keyAttributeValue) {
    NodeList nodes = root.getChildNodes();
    int len = nodes.getLength();

    for (int i = 0; i < len; i++) {
        Node node = nodes.item(i);

        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        Element element = (Element) node;

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

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

        //look inside.
        Element child = locateElement(element, tagName, keyAttributeName, keyAttributeValue);

        if (child != null)
            return child;
    }
    return null;
}

From source file:Main.java

/** Finds and returns the next sibling element node. */
public static Element getNextSiblingElement(Node node) {

    if (node == null)
        return null;
    // search for node
    Node sibling = node.getNextSibling();
    while (sibling != null) {
        if (sibling.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) sibling;
        }//from   ww  w  .j a  va2s .  com
        sibling = sibling.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

public static List<Node> extractChildElements(Node parent) {
    List<Node> result = new ArrayList<Node>();
    NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            result.add(child);//from  www  .  ja va 2s .  com
        }
    }
    return result;
}

From source file:Main.java

/**
 * @return a List of child Elements//from   w w w . ja v  a2  s .  c  o m
 */
public static List<Element> getChildren(Element element) throws Exception {
    NodeList nodes = element.getChildNodes();
    ArrayList<Element> ret = new ArrayList<Element>(nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {
        Node childNode = nodes.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            ret.add((Element) childNode);
        }
    }
    return ret;
}

From source file:Main.java

/**
 * Gets XML elements//from w  w  w.  j a  v  a  2 s  . c o  m
 * 
 * @param elementTag
 * @param fromElement
 * @return
 */
public static List getElements(String elementTag, Element fromElement) {
    List elements = new ArrayList();
    NodeList nodeList = fromElement.getElementsByTagName(elementTag);
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            elements.add((Element) node);
        }
    }
    return elements;
}

From source file:Main.java

public static List<Element> getChildElements(String tagName, Element element) {
    LinkedList<Element> result = new LinkedList<>();
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);//  w ww  .  jav a 2  s  .c o m
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tagName)) {
            result.add((Element) n);
        }
    }
    return result;
}

From source file:NodeUtils.java

/**
 * Returns a first child DOM Node of type ELEMENT_NODE
 * for the specified Node./* w  w w  . j a  v a2  s  .  com*/
 */
public static Node getChildElementNode(Node xmlNode) {
    if (xmlNode == null || !xmlNode.hasChildNodes()) {
        return null;
    }

    xmlNode = xmlNode.getFirstChild();
    while (xmlNode != null && xmlNode.getNodeType() != Node.ELEMENT_NODE) {
        xmlNode = xmlNode.getNextSibling();
    }

    return xmlNode;
}

From source file:Main.java

public static Node getChild(Node parent, String name) {
    if (parent == null) {
        return null;
    }/*from   w  w  w . j  av  a 2s.  com*/
    Node first = parent.getFirstChild();
    if (first == null) {
        return null;
    }

    for (Node node = first; node != null; node = node.getNextSibling()) {
        if (node.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        if (name != null && name.equals(node.getNodeName())) {
            return node;
        }
        if (name == null) {
            return node;
        }
    }
    return null;
}

From source file:Main.java

static boolean canBeMerged(Node node1, Node node2, String requiredTagName) {
    if (node1.getNodeType() != Node.ELEMENT_NODE || node2.getNodeType() != Node.ELEMENT_NODE)
        return false;

    Element element1 = (Element) node1;
    Element element2 = (Element) node2;

    if (!equals(requiredTagName, element1.getTagName()) || !equals(requiredTagName, element2.getTagName()))
        return false;

    NamedNodeMap attributes1 = element1.getAttributes();
    NamedNodeMap attributes2 = element2.getAttributes();

    if (attributes1.getLength() != attributes2.getLength())
        return false;

    for (int i = 0; i < attributes1.getLength(); i++) {
        final Attr attr1 = (Attr) attributes1.item(i);
        final Attr attr2;
        if (isNotEmpty(attr1.getNamespaceURI()))
            attr2 = (Attr) attributes2.getNamedItemNS(attr1.getNamespaceURI(), attr1.getLocalName());
        else//  w w w  .  java  2 s. c o  m
            attr2 = (Attr) attributes2.getNamedItem(attr1.getName());

        if (attr2 == null || !equals(attr1.getTextContent(), attr2.getTextContent()))
            return false;
    }

    return true;
}

From source file:Main.java

/**
 * Looks through all child elements of the specified root (recursively)
 * and returns the first element 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/*ww w. j a  v  a  2  s.  co  m*/
 * @param keyAttributeValue the value that attribute must have
 * @return the Element in the tree under root that matches the specified
 * paameters.
 * @throws NullPointerException if any of the arguments is null.
 */
public static Element locateElement(Element root, String tagName, String keyAttributeName,
        String keyAttributeValue) {
    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)) {
            String attr = ((Element) node).getAttribute(keyAttributeName);

            if (attr != null && attr.equals(keyAttributeValue))
                return (Element) node;
        }

        //look inside.
        Element child = locateElement((Element) node, tagName, keyAttributeName, keyAttributeValue);

        if (child != null)
            return child;

    }

    return null;
}