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

private final static boolean isParentNode(Node element) {
    NodeList elements = element.getChildNodes();
    for (int i = 0; i < elements.getLength(); i++) {
        Node child = elements.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            return true;
        }//from   ww  w.j av  a 2  s.c  o  m
    }

    return false;
}

From source file:Main.java

public static Element getNextElement(Node el) {
    while ((el != null) && (el.getNodeType() != Node.ELEMENT_NODE)) {
        el = el.getNextSibling();/* ww  w  .j  ava2  s.  co m*/
    }
    return (Element) el;

}

From source file:Main.java

public static Element getOnlyElementChild(Element e, String tag, RuntimeException exc) {
    NodeList nl = e.getChildNodes();
    Element result = null;//  ww  w .  ja  v  a2 s.c o m
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            if (result != null) // more than one Element children 
                throw exc;
            result = (Element) n;
        }
    }

    if (result == null // zero Element children
            || !result.getTagName().equals(tag)) // wrong tag
        throw exc;
    return result;
}

From source file:Main.java

public static Node getFirstChildByTagName(Node parent, String tagName) {
    NodeList nodeList = parent.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equalsIgnoreCase(tagName))
            return node;
    }/*ww  w. j  a  v  a 2  s.c  om*/
    return null;
}

From source file:Main.java

private static Node getSingleNodeElementByXpath(String xpath) throws Exception {
    NodeList list = getNodeListByXpath(xpath);
    Node node = null;//from  w  w  w  .j  a va2s.c om
    if (list.getLength() > 0 && list.item(0).getNodeType() == Node.ELEMENT_NODE) {
        node = list.item(0);
    } else {
        throw new Exception("Xpath Query did not result in a Node element. Check your Xpath expression");
    }
    return node;
}

From source file:Main.java

public static Node findSubElement(Node parent, String localName) {
    if (parent == null) {
        return null;
    }//from  www  .j  a  v a 2  s .  c  o m
    Node child = parent.getFirstChild();
    while (child != null) {
        if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getLocalName().equals(localName))) {
            return child;
        }
        child = child.getNextSibling();
    }
    return null;
}

From source file:Main.java

public static ArrayList<Element> getChildElements(Node node) {
    ArrayList<Element> l = new ArrayList<Element>();
    for (Node childNode = node.getFirstChild(); childNode != null;) {
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            Element elem = (Element) childNode;
            l.add(elem);/*from  w  w  w .  j  a va 2s .com*/
        }
        Node nextChild = childNode.getNextSibling();
        childNode = nextChild;
    }

    return l;
}

From source file:Main.java

public static Element findNextElement(final Node current, final boolean sameName) {
    String name = null;//w  w w.ja v a  2 s.c o m
    if (sameName) {
        name = current.getNodeName();
    }
    int type = Node.ELEMENT_NODE;
    return (Element) getNext(current, name, type);
}

From source file:Main.java

public static List getChildrenElement(Node n, String childTagName) {
    List lst = new ArrayList();
    NodeList nl = n.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node cn = nl.item(i);//  w  w w.  j  a  v a  2s  .co  m

        if ((cn.getNodeType() == Node.ELEMENT_NODE) && (cn.getNodeName().equals(childTagName))) {
            lst.add(cn);
        }
    }
    return lst;
}

From source file:Main.java

public static Element removeElementContentWhitespace(Element root) {
    boolean foundElt = false;
    for (Node n1 = root.getFirstChild(); n1 != null; n1 = n1.getNextSibling()) {
        if (n1.getNodeType() != Node.ELEMENT_NODE)
            continue;
        foundElt = true;/*from   w  ww  .  ja v  a2 s . c o  m*/
        removeElementContentWhitespace(Element.class.cast(n1));
    }
    if (foundElt) {
        Node n1 = root.getFirstChild();
        while (n1 != null) {
            Node n2 = n1.getNextSibling();
            if (n1.getNodeType() == Node.TEXT_NODE && isEmptyText(Text.class.cast(n1))) {
                root.removeChild(n1);
            }
            n1 = n2;
        }
    }
    return root;
}