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

/**
 * Get child elements by name, ignoring namespace declarations
 *
 * @param ele parent element/*from   w  w w  .  ja  v a  2s  . c  om*/
 * @param name name of elements to find
 */
public static List<Element> getElementsByName(Element ele, String name) {
    Vector<Element> list = new Vector();
    NodeList nl = ele.getChildNodes();
    for (int j = 0; j < nl.getLength(); j++) {
        if (nl.item(j).getNodeType() != Node.ELEMENT_NODE)
            continue;
        Element e = (Element) nl.item(j);
        String n = e.getNodeName();
        if (matches(n, name))
            list.add(e);
    }
    return (list);
}

From source file:Main.java

public static Collection<Node> getChildNodes(Node node) {
    Collection<Node> list = new LinkedList<Node>();

    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++)
        if (nl.item(i).getNodeType() == Node.ELEMENT_NODE)
            list.add(nl.item(i));//from w w  w  . j a  va  2  s  .  c o m

    return list;
}

From source file:Main.java

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

From source file:Main.java

static public Element findChildElement(Node first, Node last, String name) {
    while (first != last) {
        if (first.getNodeType() == Node.ELEMENT_NODE) {
            if (first.getNodeName().equals(name))
                return (Element) first;
        }//from w w  w .ja  va2  s . c o m
        first = first.getNextSibling();
    }
    return null;
}

From source file:Main.java

/**
 * Gets the child elements of a parent element. Unlike DOM's getElementsByTagName, this does no recursion,
 * uses local name (namespace free) instead of tag name, result is a proper Java data structure and result
 * needs no casting. In other words, this method does not suck unlike DOM.
 * /*from www .  ja  v a  2  s  .  c  o m*/
 * @param parent the XML parent element
 * @param name name of the child elements, if null then all are returned
 */
public static List<Element> getChildElements(Element parent, String name) {
    List<Element> childElements = new ArrayList<Element>();
    NodeList childNodes = parent.getChildNodes();

    for (int i = 0; i < childNodes.getLength(); i++) {
        // get elements
        if (childNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {

            // match element name
            Element childElement = (Element) childNodes.item(i);
            if (name == null || childElement.getLocalName().equals(name)) {
                childElements.add(childElement);
            }
        }
    }

    return childElements;
}

From source file:Main.java

/**
 * returns list of child Element Nodes for a given, parsed XML node
 * /*from   w w  w.ja v a  2s. c  o  m*/
 * @param xmlNode parsed XML Node for which child Nodes are to be returned
 * @return List of child Element Nodes of xmlNode
 **/
public static final List<Element> getChildElementNodes(final Node xmlNode) {
    final NodeList children = xmlNode.getChildNodes();
    if (children == null) {
        return null;
    }
    final int childrenCnt = children.getLength();
    final List<Element> list = new ArrayList<Element>(childrenCnt);

    for (int i = 0; i < childrenCnt; i++) { // Then loop through them looking for search criteria node
        final Node childNode = children.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            list.add((Element) childNode);
        }
    }

    return list;
}

From source file:Main.java

public static List<Node> getNodesWithName(Node parent, String name, boolean all_of_them) {
    ArrayList<Node> valid = new ArrayList<>();
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); i++)
        if (children.item(i).getNodeType() == Node.ELEMENT_NODE
                && (name == null || children.item(i).getNodeName().equalsIgnoreCase(name))) {
            valid.add(children.item(i));
            if (!all_of_them)
                break;
        }//  w  w  w. j  ava 2 s  .c  om
    return valid;
}

From source file:Main.java

public static String logElement(Element El, int level) {

    String Es = "";
    String indentText = "  ";
    String addIndT = "";

    // add indent
    int ind = 0;//from   ww  w .  j a va  2s  .c om
    while (ind < level) {
        addIndT = addIndT + indentText;
        ind++;

    }
    String name = El.getNodeName();
    Es = "\n" + addIndT + "<" + name + " ";
    // Attribs
    NamedNodeMap namedNodeMap = El.getAttributes();
    StringBuilder sb = new StringBuilder(Es);
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Attr att = (Attr) namedNodeMap.item(i);
        sb.append(" " + Es + att.getName() + "=\"" + att.getNodeValue() + "\" ");
        // Es = " " + Es + att.getName() + "=\"" + att.getNodeValue() +
        // "\" ";
    }
    sb.append(">");
    // Es = Es + ">";
    Es = sb.toString();
    NodeList pl = El.getChildNodes();
    int index = pl.getLength();
    // text nodes
    if (index > 0) {
        int i = 0;
        while (i < index) {
            Node DomNode = pl.item(i);
            if ((DomNode.getNodeType()) == org.w3c.dom.Node.TEXT_NODE) {
                String Etext = DomNode.getNodeValue();
                Es = Es + "\n " + addIndT + addIndT + Etext;
            }
            i++;
        }
    }
    // Child Elements
    if (index > 0) {
        level++;
        int i = 0;
        while (i < index) {
            Node DomNode = pl.item(i);
            if ((DomNode.getNodeType()) == org.w3c.dom.Node.ELEMENT_NODE) {
                Element el = (Element) DomNode;
                Es = Es + logElement(el, level);
            }
            i++;
        }
    }
    Es = Es + "\n" + addIndT + "</" + name + ">";

    return Es;
}

From source file:Main.java

public static List<Element> getChildElements(Element parent) {
    if (null == parent)
        return null;

    NodeList nodes = parent.getChildNodes();
    List<Element> elements = new ArrayList<Element>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE)
            elements.add((Element) node);
    }/*from ww w . j  av a2s .  c  om*/

    return elements;
}

From source file:Main.java

/**
 * Get the list of all the elements of a parent element.
 * @param parentElement the parent element
 * @param elementName the name of the element
 * @return the list of the elements//from   w w w.j  av  a  2  s .c om
 */
public static List<Element> getElementsByTagName(final Element parentElement, final String elementName) {

    if (elementName == null || parentElement == null) {
        return null;
    }

    final NodeList nStepsList = parentElement.getElementsByTagName(elementName);
    if (nStepsList == null) {
        return null;
    }

    final List<Element> result = new ArrayList<>();

    for (int i = 0; i < nStepsList.getLength(); i++) {

        final Node node = nStepsList.item(i);

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

    return result;
}