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 nextSiblingElement(Node node) {
    for (Node tempNode = node.getNextSibling(); tempNode != null; tempNode = tempNode.getNextSibling()) {
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) tempNode;
        }//  ww w  . ja  v a2 s  .c o  m
    }
    return null;
}

From source file:Main.java

/**
 * Get the first found child element with the provided local name that is a
 * direct child the provided element.//from  ww w . ja va2 s.co  m
 * 
 * @param parent
 *            The parent element
 * @param name
 *            The name of the child element to find
 * @return The first found child element, null if no matching children
 */
public static Element getFirstChildElementByLocalName(Element parent, String localName) {
    assertNotNull(parent);
    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.getLocalName().equals(localName)) {
            return (Element) node;
        }
    }

    return null;
}

From source file:Main.java

public static Element lastChildElement(Node node) {
    for (Node tempNode = node.getLastChild(); tempNode != null; tempNode = tempNode.getPreviousSibling()) {
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) tempNode;
        }// ww w. j  a v a  2 s  .c om
    }
    return null;
}

From source file:Main.java

/**
 * Get first child element with the provided node name and attribute that
 * are direct child the provided element.
 * /* w w  w.j  av a 2  s.co  m*/
 * @param parent
 * @param name
 * @param attributeName
 * @param attributeValue
 * @return element if found, otherwise null
 */
public static Element getChildElementByNameAndAttribute(Element parent, String name, String attributeName,
        String attributeValue) {
    assertNotNull(parent);

    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)) {
            Element element = (Element) node;
            if (element.getAttribute(attributeName).equals(attributeValue)) {
                return element;
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * Returns a List of all child Elements of the given Element.
 *
 * @param element the element from which to retrieve child elements
 * @return a List of child Elements./*ww  w.  ja  v  a 2  s  .c  o  m*/
 * @throws NullPointerException if the element is null.
 */
public static List getChildElements(Element element) {
    if (element == null)
        throw new NullPointerException("Tried to get child elements on a null element");

    List result = new ArrayList();

    NodeList children = element.getChildNodes();

    for (int i = 0; i != children.getLength(); i++) {
        if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
            result.add(children.item(i));
        }
    }

    return result;
}

From source file:Main.java

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

From source file:Main.java

/** Recupera el &iacute;ndice siguiente nodo de la lista de tipo Element. Empieza a comprobar
 * los nodos a partir del &iacute;ndice marcado. Si no encuentra un nodo de tipo elemento,
 * devuelve -1./*from  www.j  a va  2s.  co  m*/
 * @param nodes Listado de nodos.
 * @param currentIndex &Iacute;ndice del listado a partir del cual se empieza la comprobaci&oacute;n.
 * @return &Iacute;ndice del siguiente node de tipo Element o -1 si no se encontr&oacute;.
 */
static int nextNodeElementIndex(final NodeList nodes, final int currentIndex) {
    Node node;
    int i = currentIndex;
    while (i < nodes.getLength()) {
        node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            return i;
        }
        i++;
    }
    return -1;
}

From source file:Main.java

static public ArrayList<Element> selectElements(Element element, String xpathExpression) throws Exception {
    ArrayList<Element> resultVector = new ArrayList<Element>();
    if (element == null) {
        return resultVector;
    }//w w w  .  jav a  2s.  c  o  m
    if (xpathExpression.indexOf("/") == -1) {
        NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(xpathExpression)) {
                resultVector.add((Element) node);
            }
        }
    } else {
        XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList nodes = (NodeList) xpath.evaluate(xpathExpression, element, XPathConstants.NODESET);

        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            resultVector.add((Element) node);
        }
    }
    return resultVector;
}

From source file:Main.java

public static Element[] getSubChildElement(Element ele, String[] tagnames) {
    if (ele == null) {
        return null;
    }/*w  ww.  ja v a2s.  c o  m*/

    List<Element> v = new ArrayList<Element>();

    NodeList tmpnl = ele.getChildNodes();

    Node tmpn = null;

    int k;
    for (k = 0; k < tmpnl.getLength(); k++) {
        tmpn = tmpnl.item(k);

        if (tmpn.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }

        Element eee = (Element) tmpn;
        String noden = eee.getNodeName();
        int p = noden.indexOf(':');
        if (p >= 0)
            noden = noden.substring(p + 1);

        for (int i = 0; i < tagnames.length; i++) {
            if (tagnames[i].equals(noden) || tagnames[i].equals("*")) {
                v.add(eee);
                break;
            }
        }
    }

    Element[] rets = new Element[v.size()];
    v.toArray(rets);
    return rets;
}

From source file:Main.java

public static boolean isElement(Node n) {
    return (n.getNodeType() == Node.ELEMENT_NODE);
}