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 childNodeByTag(Node node, String childNodeName) {
    if (node == null)
        return null;

    Element childElement = null;/*w ww . j  a v  a2s  .com*/
    Node childNode = node.getFirstChild();
    if (childNode != null) {
        do {
            if (childNode.getNodeType() == Node.ELEMENT_NODE
                    && (childNodeName == null || childNodeName.equals(childNode.getNodeName()))) {
                return (Element) childNode;
            }
        } while ((childNode = childNode.getNextSibling()) != null);
    }

    return null;
}

From source file:Main.java

public static Element getNextElement(Node el) {
    Node node = el;//  ww  w  . j a  va  2s. c  om
    while (node != null && node.getNodeType() != Node.ELEMENT_NODE) {
        node = node.getNextSibling();
    }
    return (Element) node;
}

From source file:Main.java

public static Element getChild(Element parent, String name) {

    NodeList children = parent.getChildNodes();

    final int length = children.getLength();

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

        Node node = children.item(index);

        if (node.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }/*from  ww w  .  j a va2s  .  co  m*/

        Element element = (Element) node;

        if (name.equalsIgnoreCase(element.getNodeName())) {
            return element;
        }
    }

    return null;
}

From source file:Main.java

public static Node getFirstChildElementNode(Node node) {
    if (node == null)
        return (null);

    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (Node.ELEMENT_NODE == child.getNodeType())
            return (child);
    }// w w  w  . j  ava  2s .c o m

    return (null);
}

From source file:Main.java

/**
 * Gets the content of a subnode./*from   w w w .  j  ava2 s  .c om*/
 * For example,
 * <item>
 *     <nodeName>content</nodeName>
 * </item>
 */
public static Optional<String> getNodeContent(Node item, String nodeName) {
    if (item.getNodeType() != Node.ELEMENT_NODE) {
        return Optional.empty();
    }

    NodeList metadata = ((Element) item).getElementsByTagName(nodeName);
    if (metadata.getLength() == 1) {
        return Optional.ofNullable(metadata.item(0).getTextContent());
    } else {
        return Optional.empty();
    }
}

From source file:Main.java

public static boolean updateTextNode(String nodeName, Element searchFrom, String data, int position)
        throws Exception {
    boolean result = false;
    Element currentElement = (Element) searchFrom.getElementsByTagName(nodeName).item(position);

    if (currentElement != null && currentElement.getNodeType() == Node.ELEMENT_NODE) {
        Text textNode = (Text) (currentElement.getFirstChild());
        textNode.setData(data);//  w  ww. j  a  va2 s  .com

        if (textNode != null && textNode.getNodeValue() == null)
            result = false;
        else
            result = true;
    }
    return result;
}

From source file:Main.java

/**
 * Gets the first child element of an element
 *
 * @param element the parent// w w  w  .j  a  v a  2s.c om
 * @return the first child element or null if there isn't one
 */
public static Element getFirstChildElement(Element element) {
    Node child = element.getFirstChild();
    while (child != null && child.getNodeType() != Node.ELEMENT_NODE)
        child = child.getNextSibling();

    return (Element) child;
}

From source file:Main.java

public static List<Node> getChildrenByTagName(Node parent, String tagName) {
    List<Node> eleList = new ArrayList<Node>();
    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)) {
            eleList.add(node);/*from   www  .  j a va  2  s .co m*/
        }

    }
    return eleList;
}

From source file:Main.java

public static List<Element> childs(Node node) {
    NodeList children = node.getChildNodes();
    List<Element> result = new LinkedList<Element>();
    for (int i = 0; i < children.getLength(); i++) {
        Node c = children.item(i);
        if (c.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }//  w  w w . j  a  va2  s.c om
        result.add((Element) c);
    }
    return result;
}

From source file:Main.java

/**
 * Returns a list of elements having a given tag
 *//* ww  w .j  a  va2  s .  co  m*/
public static List<Element> getElements(Element element, String tagName) {
    Node node = element.getFirstChild();
    List<Element> elements = new ArrayList<Element>();

    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().compareTo(tagName) == 0)
            elements.add((Element) node);

        node = node.getNextSibling();
    }

    return elements;
}