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

/**
 * Returns true if element node has children elements
 * @param actElement/*from   w  w  w  .  j a va 2 s. c o  m*/
 * @return
 */
protected static boolean hasChildElementNodes(Element actElement) {
    try {
        if (actElement.hasChildNodes()) {
            NodeList childNodes = actElement.getChildNodes();

            for (int i = 0; i < childNodes.getLength(); i++) {
                Node element = childNodes.item(i);
                if (element.getNodeType() == Node.ELEMENT_NODE) {
                    return true;
                }
            }
        }
        return false;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static Element getElementByTagNameAndAttributeValue(Element element, String tagName, String attrName,
        String attrValue) {//w ww. j  av a  2s  . c o  m
    NodeList nodeList = element.getElementsByTagName(tagName);
    if (nodeList.getLength() == 0) {
        return null;
    }
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element nodeElement = (Element) node;
            if (attrValue.equals(getAttributeValueByName(nodeElement, attrName))) {
                return nodeElement;
            }
        }
    }
    return null;
}

From source file:Main.java

public static List<Element> getChildren(Element parent) {

    NodeList children = parent.getChildNodes();

    final int length = children.getLength();

    ArrayList<Element> elements = new ArrayList<Element>();

    for (int index = 0; index < length; index++) {

        Node node = children.item(index);

        if (node.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }/*from w w w.j a va2 s . c o m*/

        elements.add((Element) node);
    }

    return elements;
}

From source file:Main.java

/**
 * Convenience method for setting a node's (attr or elem) text value.
 * /*from w  ww .  j  av a2  s  . c  o m*/
 * @param node     The DOM node whose value needs to be changed.
 * @param newValue The value to set for node.
 */
public static void setNodeValue(final Node node, final String newValue) {
    short nodeType = node.getNodeType();
    if (nodeType == Node.ATTRIBUTE_NODE) {
        node.setNodeValue(newValue);
    } else if (nodeType == Node.ELEMENT_NODE) {
        node.setTextContent(newValue);
    }
}

From source file:Main.java

public static List<Element> getElementCollectionFromDocument(Document document, String tagName) {
    List<Element> res = new LinkedList<Element>();
    NodeList entries = document.getElementsByTagName(tagName);
    for (int i = 0; i < entries.getLength(); i++) {
        Node entry = entries.item(i);
        if (entry.getNodeType() == Node.ELEMENT_NODE) {
            res.add((Element) entry);
        }//from   w w  w  .j av a  2  s  . co  m
    }
    return res;
}

From source file:Main.java

public static Map<String, String> getTagValueMap(Element ele, String language) {
    Map<String, String> map = new LinkedHashMap<String, String>();
    NodeList children = ele.getChildNodes();
    Node current = null;/*  w  ww  .  ja  v a 2s .  c o  m*/
    int count = children.getLength();
    for (int i = 0; i < count; i++) {
        current = children.item(i);
        if (current.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) current;
            map.put(current.getNodeName(), element.getFirstChild().getNodeValue());
        }
    }
    return map;
}

From source file:Main.java

public static Element getChild(Element node, String tagName) {
    Element element = null;//from   w w  w .j  ava 2 s.  c  om

    do {
        if (node == null) {
            break;
        }

        NodeList childNodes = node.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);

            if (child.getNodeType() == Node.ELEMENT_NODE) {
                if (child.getNodeName().equals(tagName)) {
                    element = (Element) child;
                    break;
                }
            }
        }
    } while (false);

    return element;
}

From source file:Main.java

/**
 * @param n/*  w  w  w.jav  a2s . c om*/
 *            the node
 * @return the first element in the ancestor tree of {@code n}. If
 *         {@code n} is an {@link Element}, {@code n} is returned. If
 *         {@code n} is <code>null</code>, this method returns
 *         <code>null</code>.
 */
public static Element getAncestorElement(Node n) {
    if (n == null) {
        return null;
    }
    if (n.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) n;
    }
    Node parent = n.getParentNode();
    if (parent == null) {
        Document doc = (Document) (n instanceof Document ? n : n.getOwnerDocument());
        return doc == null ? null : doc.getDocumentElement();
    }
    return getAncestorElement(parent);
}

From source file:Main.java

/**
 * Returns the first node in the node list that is an element.
 * /*from  w w w  . ja  va 2 s.  c o  m*/
 * @param nodes The list of nodes that contains the element to be
 *              determined.
 * 
 * @return The first element in the given node list.
 */
public static Node getFirstElement(NodeList nodes) {
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            return node;
        }
    }
    return null;
}

From source file:MainClass.java

public static void print(Node node, OutputStream os) {
    PrintStream ps = new PrintStream(os);
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        ps.print("<" + node.getNodeName());

        NamedNodeMap map = node.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            ps.print(" " + map.item(i).getNodeName() + "=\"" + map.item(i).getNodeValue() + "\"");
        }/*from w  w  w .  j a v a2 s . com*/
        ps.println(">");
        return;
    case Node.ATTRIBUTE_NODE:
        ps.println(node.getNodeName() + "=\"" + node.getNodeValue() + "\"");
        return;
    case Node.TEXT_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.CDATA_SECTION_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.PROCESSING_INSTRUCTION_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
        ps.println(node.getNodeName() + "=" + node.getNodeValue());
        return;
    }
}