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

/**
 * Return all values associated with attributeName inside element specified by tag
 * @param document doc we work with/*from   w w  w  .ja va  2  s  .c  om*/
 * @param elementTag element containing desired attribute
 * @param attributeName name of attribute
 * @return List of values specified in XML array
 */
public static List<String> getListOfTagAttributeValues(Document document, String elementTag,
        String attributeName) {
    NodeList usesPermissionList = document.getElementsByTagName(elementTag);

    List<String> result = new ArrayList<String>();
    for (int i = 0; i < usesPermissionList.getLength(); i++) {
        Node nNode = usesPermissionList.item(i);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            String value = eElement.getAttribute(attributeName);
            if (value != null && !value.isEmpty()) {
                result.add(value);
            }
        }
    }
    return result;
}

From source file:Main.java

/**
 * Return the first child Element with the given name; if name is null returns the first element.
 *///from ww  w .  j ava2 s .  c  o  m
public static Element firstChildElement(Element element, String childElementName) {
    if (element == null)
        return null;
    // get the first element with the given name
    Node node = element.getFirstChild();

    if (node != null) {
        do {
            if (node.getNodeType() == Node.ELEMENT_NODE
                    && (childElementName == null || childElementName.equals(node.getNodeName()))) {
                Element childElement = (Element) node;

                return childElement;
            }
        } while ((node = node.getNextSibling()) != null);
    }
    return null;
}

From source file:Main.java

public static Element getFirstChildElementIgnoreCase(Element elm, String name) {
    if (elm == null)
        return null;

    NodeList nl = elm.getChildNodes();
    for (int c = 0; c < nl.getLength(); c++) {
        Node node = nl.item(c);/*from ww  w  .j a  va2  s.c o  m*/
        if (node.getNodeType() == Node.ELEMENT_NODE
                && (name == null || node.getNodeName().equalsIgnoreCase(name)))
            return (Element) node;
    }

    return null;
}

From source file:Main.java

public static Node cloneNode(Document d, Node n) {
    Node r = null;//w  w  w  .j  a  v a  2s  .  c  o  m
    switch (n.getNodeType()) {
    case Node.TEXT_NODE:
        r = d.createTextNode(((Text) n).getData());
        break;
    case Node.CDATA_SECTION_NODE:
        r = d.createCDATASection(((CDATASection) n).getData());
        break;
    case Node.ELEMENT_NODE:
        r = d.createElement(((Element) n).getTagName());
        NamedNodeMap map = n.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            ((Element) r).setAttribute(((Attr) map.item(i)).getName(), ((Attr) map.item(i)).getValue());
        }
        break;
    }
    return r;
}

From source file:Main.java

public static Element getNextSibling(Element e) {
    Node n = e.getNextSibling();/*from  w  w  w . ja va 2s  .c  o  m*/
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
        n = n.getNextSibling();
    return (Element) n;
}

From source file:Main.java

/**
 *  Returns first node at the bottom of path from node.
 * If element begins with '@', indicates an attribute, eg "@id"
 * The '#text' element indicates that the node has a single text child.
 * @param node    Node to apply path to/*from   w w  w. ja  va 2  s  .  c o m*/
 * @param path    Path to apply
 * @return        Node at bottom of path, or null
 */
static public Node extractNode(Node node, String path) {
    if (node == null)
        return null;
    NodeList list = node.getChildNodes();
    if (path.equals("#text"))
        return node.getFirstChild();
    else if (path.charAt(0) == '@')
        return node.getAttributes().getNamedItem(path.substring(1));
    else
        for (int j = 0; j < list.getLength(); j++)
            if (list.item(j).getNodeType() == Node.ELEMENT_NODE && list.item(j).getNodeName().equals(path))
                return list.item(j);

    return null;
}

From source file:Main.java

/**
 * Get all child elements with the provided local name that are direct
 * children the provided element.//w  ww. ja va  2 s.co m
 * 
 * @param parent
 *            The parent element
 * @param name
 *            The local name of the child elements to find
 * @return The list with child elements, empty list if no matching children
 */
public static Collection<Element> getChildElementsByLocalName(Element parent, String name) {
    assertNotNull(parent);
    Collection<Element> childList = new ArrayList<Element>();

    NodeList children = parent.getChildNodes();
    Node node;
    for (int i = 0; i < children.getLength(); i++) {
        node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getLocalName().equals(name)) {
            childList.add((Element) node);
        }
    }

    return childList;
}

From source file:Main.java

/** Finds and returns the last child node with the given name. */
public static Element getLastChildElement(Node parent, String elemName) {

    if (parent == null)
        return null;
    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            if (child.getNodeName().equals(elemName)) {
                return (Element) child;
            }/*w ww  .  ja va2s.co m*/
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/**
 * This method is used to search all the <code>Element</code> objects with
 * given key in the passed <code>Element</code> object.
 * /*from ww  w.ja va2s. c  om*/
 * @param tagName
 *            Name of the tag as String.
 * @param input
 *            Element object.
 * @return <code>Element[]</code> Returns the array of elements, or an empty
 *         array in case here is no match.
 */
public static List<Element> getElements(String tagName, Element input) {
    NodeList nodes = input.getElementsByTagName(tagName);

    int len = nodes.getLength();
    List<Element> elt = new ArrayList<Element>(len);
    Node node;
    for (int i = 0; i < len; i++) {
        node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            elt.add((Element) node);
        }
    }

    return elt;
}

From source file:Main.java

/**
 * This method extracts the list of elements from the @param element that contains the @param tagName.
 *
 * @param tagName the tag name used to get the list of elements
 * @param element the dom element to look for the tags with the given name
 * @return list of elements that have the @param tagName
 *//* ww w  . j a  v a 2 s  . c  om*/
public static List<Element> getElementsByTagName(String tagName, Element element) {
    List<Element> elements = new ArrayList<>();

    NodeList nodeList = element.getElementsByTagName(tagName);

    if (nodeList != null && nodeList.getLength() > 0) {

        for (int index = 0; index < nodeList.getLength(); index++) {
            Node currentNode = nodeList.item(index);

            if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                elements.add((Element) currentNode);
            }
        }
    }
    return elements;
}