Example usage for org.w3c.dom Element getNodeName

List of usage examples for org.w3c.dom Element getNodeName

Introduction

In this page you can find the example usage for org.w3c.dom Element getNodeName.

Prototype

public String getNodeName();

Source Link

Document

The name of this node, depending on its type; see the table above.

Usage

From source file:Main.java

public static NodeList getChildsByTagName(Element root, String name) {
    final Vector<Node> v = new Vector<Node>();
    NodeList nl = root.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);//  w w w . j  a v  a 2 s  . c o  m
        if (n.getNodeType() == Element.ELEMENT_NODE) {
            Element e = (Element) n;
            if (name.equals("*") || e.getNodeName().equalsIgnoreCase(name))
                v.add(n);
        }
    }

    return new NodeList() {
        public Node item(int index) {
            if (index >= v.size() || index < 0)
                return null;
            else
                return v.get(index);
        }

        public int getLength() {
            return v.size();
        }
    };
}

From source file:Main.java

public static String getChildElementValueByTagName(Element parentElement, String childTag) {
    if (childTag.equals(parentElement.getNodeName())) {
        return getNodeValue(parentElement);
    }/*from ww w. j  a v a2  s.  com*/
    for (Node temp = parentElement.getFirstChild(); temp != null; temp = temp.getNextSibling()) {
        if (temp.getNodeType() == Node.ELEMENT_NODE && childTag.equals(temp.getNodeName())) {
            return getNodeValue(temp);
        }
    }
    return null;
}

From source file:Main.java

private static boolean isJsModuleElement(Element element) {
    String elementName = element.getNodeName();
    return elementName.equals(TAG_JS_MODULE);
}

From source file:Main.java

/**
 * Returns the child element with the specified name for the specified element.
 *
 * @param element the parent element/*from   w w  w  . j ava2 s.c  om*/
 * @param name    the name of the child element to return
 *
 * @return the child element or <code>null</code> if a child element with the specified name
 *         could not be found
 */
public static Element getChildElement(Element element, String name) {
    NodeList nodeList = element.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);

        if (node instanceof Element) {
            Element childElement = (Element) node;

            if (childElement.getNodeName().equals(name)) {
                return childElement;
            }
        }
    }

    return null;
}

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;
        }/* ww w.j  a va2s .  c o m*/

        Element element = (Element) node;

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

    return null;
}

From source file:Main.java

/**
 * Returns <code>true</code> if the specified element has a child with the specified name or
 * <code>false</code> otherwise.
 *
 * @param element the parent element//from   w ww  . j  a  v  a 2  s  .  c om
 * @param name    the name of the child element
 *
 * @return <code>true</code> if the specified element has a child with the specified name or
 *         <code>false</code> otherwise
 */
public static boolean hasChildElement(Element element, String name) {
    NodeList nodeList = element.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);

        if (node instanceof Element) {
            Element childElement = (Element) node;

            if (childElement.getNodeName().equals(name)) {
                return true;
            }
        }
    }

    return false;
}

From source file:Main.java

/**
 * Returns the text content for the child element with the specified name for the specified
 * element./*from  w w w . ja  v  a2 s  . co  m*/
 *
 * @param element the parent element
 * @param name    the name of the child element to return
 *
 * @return the text content for the child element or <code>null</code> if a child element with
 *         the specified name could not be found
 */
public static String getChildElementText(Element element, String name) {
    NodeList nodeList = element.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);

        if (node instanceof Element) {
            Element childElement = (Element) node;

            if (childElement.getNodeName().equals(name)) {
                return childElement.getTextContent();
            }
        }
    }

    return null;
}

From source file:Main.java

public static Element getElementByTagName(Node element, String name) {
    NodeList elements = element.getChildNodes();
    if (elements != null) {
        for (int i = 0; i < elements.getLength(); i++) {
            if (elements.item(i).getNodeType() == Node.ELEMENT_NODE) {
                Element currentElement = (Element) elements.item(i);
                if (currentElement.getNodeName().equals(name)) {

                    return currentElement;
                }//from   w  ww. j ava  2  s .  c  o m
            }
        }
    }

    return null;
}

From source file:Main.java

public static String getNodeValue(final NodeList nodes, final String strNodeName,
        final String strDefaultValue) {
    for (int i = 0; i < nodes.getLength(); ++i) {
        final Node node = nodes.item(i);

        if (node instanceof Element) {
            final Element elem = (Element) node;

            if (elem.getNodeName().equals(strNodeName)) {
                return elem.getLastChild().getTextContent().trim();
            }/* www  .  j a  va2 s  . c o  m*/
        }
    } // end for

    return strDefaultValue;
}

From source file:Main.java

/**
 * Returns the child elements with the specified name for the specified element.
 *
 * @param element the parent element//from   w  w  w  .j  a v  a 2s  .c om
 * @param name    the name of the child elements to return
 *
 * @return the child elements with the specified name for the specified element
 */
public static List<Element> getChildElements(Element element, String name) {
    List<Element> childElements = new ArrayList<>();

    NodeList nodeList = element.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);

        if (node instanceof Element) {
            Element childElement = (Element) node;

            if (childElement.getNodeName().equals(name)) {
                childElements.add(childElement);
            }
        }
    }

    return childElements;
}