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 List<Node> getChildNodeList(Node parentNode, String nodeName) {
    List<Node> childNodeList = new ArrayList<>();
    NodeList nodeList = parentNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (((Element) childNode).getNodeName().equals(nodeName)) {
                childNodeList.add(childNode);
            }//from   w w w .j  a  v a2s . co  m
        }
    }
    return childNodeList;
}

From source file:Main.java

public static String[] getChildrenText(Element parentElement, String childrenName) {
    NodeList nl = parentElement.getChildNodes();
    int max = nl.getLength();
    LinkedList<String> list = new LinkedList<String>();
    for (int i = 0; i < max; i++) {
        Node n = nl.item(i);//  w w  w  .jav a2 s  .c  om
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(childrenName)) {
            list.add(getText((Element) n));
        }
    }
    return list.toArray(new String[list.size()]);
}

From source file:Main.java

public static List<Element> extractElementsFromNodeList(NodeList config, String tag, boolean required) {
    List<Element> res = new ArrayList<Element>();
    for (int i = 0; i < config.getLength(); i++) {
        Node n = config.item(i);/*from w w w.  j a  v a  2s . co m*/
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tag)) {
            res.add((Element) config.item(i));
        }
    }
    if (required && res.size() == 0) {
        throw new RuntimeException("Tag " + tag + " is required in configuration file");
    }
    return res;
}

From source file:Main.java

public static List getChildElements(Element parent, String childName) {
    NodeList children = parent.getChildNodes();
    List list = new ArrayList();
    int size = children.getLength();

    for (int i = 0; i < size; i++) {
        Node node = children.item(i);

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

            if (childName.equals(element.getNodeName())) {
                list.add(element);/*w w w . j  a  v  a2s .  c o  m*/
            }
        }
    }

    return list;
}

From source file:Main.java

public static Element getChildElementByTagName(Element parentElement, String childTag) {
    for (Node temp = parentElement.getFirstChild(); temp != null; temp = temp.getNextSibling())
        if (temp.getNodeType() == Node.ELEMENT_NODE && childTag.equals(temp.getNodeName())) {
            return (Element) temp;
        }/*from w  ww. j  a v  a  2s . com*/
    return null;
}

From source file:Main.java

public static Map<String, String> NodeListToMap(NodeList nList) {
    Map<String, String> simpleMap = new HashMap<String, String>();
    for (int i = 0; i < nList.getLength(); i++) {
        Node nNode = nList.item(i);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            if (eElement != null) {
                simpleMap.put(eElement.getTagName(), eElement.getTextContent());
            } // end if eelement
        } // end if nnode.getnodetype()
    } //end for int temp
    return simpleMap;
}

From source file:Main.java

public static List<Element> getChildElemsListByName(final String name, final Node parent) {
    ArrayList<Element> v = new ArrayList<Element>();
    Element elem = null;//from  w ww . j av  a2  s.c  om
    for (Node childNode = parent.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) {
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (childNode.getNodeName() == name) {
                elem = (Element) childNode;
                v.add(elem);
            }
        }
    }
    return v;
}

From source file:Main.java

public static List<Element> getElements(final Element element, final String tagName) {
    final ArrayList<Element> elements = new ArrayList<>();
    final NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        final Node node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            final String nodeName = node.getNodeName();
            if ((nodeName != null) && nodeName.equals(tagName)) {
                elements.add((Element) node);
            }//from   w  ww .ja  v a 2  s  .co  m
        }
    }
    return elements;
}

From source file:Main.java

public static List<Element> getElementCollectionByTagName(Element element, String tagName) {
    List<Element> result = new LinkedList<Element>();
    NodeList nodeList = element.getElementsByTagName(tagName);
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            result.add((Element) node);
        }//w  w  w.ja v a  2 s  .  c o m
    }
    return result;
}

From source file:Main.java

public static ArrayList<Element> getElements(String elementName, Document doc) {
    ArrayList<Element> result = new ArrayList<Element>();
    NodeList list = doc.getElementsByTagName(elementName);

    for (int i = 0; i < list.getLength(); i++) {
        Node nNode = list.item(i);

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            result.add((Element) nNode);
        }/*  w  ww  .  j ava 2 s. c  om*/
    }
    return result;
}