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 getChildElement(Element elem, String name) {
    NodeList nodes = elem.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) {
            return (Element) node;
        }/*from   w  w  w.j av  a2s.co  m*/
    }
    return null;
}

From source file:Main.java

public static Element findElement(Node sourceElm, String attrName, String sAttrValue) {
    if (sourceElm.hasChildNodes()) {
        NodeList nodes = sourceElm.getChildNodes();

        for (int i = 0; i < nodes.getLength(); i++) {
            if (Node.ELEMENT_NODE == nodes.item(i).getNodeType()) {
                Element elm = findElement(nodes.item(i), attrName, sAttrValue);

                if (((Element) elm).getAttribute(attrName).equals(sAttrValue)) {
                    return (Element) elm;
                }//w w  w .  j  av a2  s .c  o  m
            }
        }
    } else {
        if (sourceElm.hasAttributes() && ((Element) sourceElm).getAttribute(attrName).equals(sAttrValue)) {
            return (Element) sourceElm;
        }
    }

    return (Element) sourceElm;
}

From source file:Main.java

public static Map<String, String> getChildTextMap(Element elt) {
    Map<String, String> data = new HashMap<String, String>();
    NodeList nodes = elt.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node child = nodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element e = (Element) child;
            data.put(e.getNodeName(), getInnerText(e));
        }/*from w  ww  . j ava 2  s  .com*/
    }
    return data;
}

From source file:Main.java

/**
 *   Find the first child Element matching the given tag name. Only searches 
 *   1st-level children; not a recursive search. Null if not found.
 *//*  w  w  w. jav  a  2 s  .c o  m*/
public static Element findChildElementMatching(Node root, String tagName) {
    return (Element) findChildNodeMatching(root, tagName, Node.ELEMENT_NODE);
}

From source file:Main.java

public static Element getElement(Element parentNode, String nodeName) {
    if (parentNode == null) {
        return null;
    }//from w ww.ja va  2 s .c  om
    NodeList nl = parentNode.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
            if (nl.item(i).getNodeName().equals(nodeName)) {
                return (Element) nl.item(i);
            }
        }
    }
    return null;
}

From source file:Main.java

public static Element getChildByName(Element parent, String nodeName) {
    NodeList nodes = parent.getChildNodes();
    int len = nodes.getLength();
    if (nodes != null && len > 0) {
        for (int i = 0; i < len; i++) {
            if (nodes.item(i).getNodeType() == Node.ELEMENT_NODE
                    && nodes.item(i).getNodeName().equals(nodeName)) {
                return (Element) nodes.item(i);
            }//  w  w  w. j ava 2 s.  c o  m
        }
    }
    return null;
}

From source file:Main.java

/**
 * @param node//from  www.  j  a v  a 2 s.c o  m
 */
public static void displayNodeInfo(Node node) {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        System.out.println("Document Node ");
        break;
    case Node.ELEMENT_NODE:
        System.out.println("Element Node: " + node.getNodeName());
        break;
    case Node.TEXT_NODE:
        System.out.println("Text Node: " + node.getNodeName());
        break;
    case Node.CDATA_SECTION_NODE:
        System.out.println("CDATA Section Node: ");
        break;
    case Node.COMMENT_NODE:
        System.out.println("Comment Node ");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println("Processing Instruction Node ");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.println("Entity Reference Node ");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        System.out.println("Document Type Node ");
        break;
    }
}

From source file:Main.java

public static Node getChildNode(Node parentNode, String nodeName) {
    Node node = null;// w w w .  j  ava 2s.  c o  m
    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)) {
                node = childNode;
                break;
            }
        }
    }
    return node;
}

From source file:Main.java

public static Element getChildElementByTagNameNS(Element parent, String tagName, String nsName) {
    NodeList nl = parent.getChildNodes();
    int size = nl.getLength();
    for (int i = 0; i < size; i++) {
        Node node = nl.item(i);/*from   w  w w. j a v a  2  s  .c  o m*/
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (tagName.equals(node.getLocalName())) {
                String ns = node.getNamespaceURI();
                if (ns != null && ns.equals(nsName)) {
                    return (Element) node;
                }
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * Returns first DOM node of type element contained within given element.
 *  //from w w w .  jav a 2s . c  o  m
 * @param elem element
 */
public static Element getContainedElement(Element elem) {
    Node n = elem.getFirstChild();
    while (n != null) {
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            Element e = (Element) n;
            return e;
        }
        n = n.getNextSibling();
    }
    return null;
}