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

/**
 * Determine whether an element has any child elements.
 * @param element the element to check.//from   w ww  .j a  v  a2 s  .  com
 * @return true if the element has child elements; false otherwise.
 */
public static boolean hasChildElements(Element element) {
    Node child = element.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE)
            return true;
        child = child.getNextSibling();
    }
    return false;
}

From source file:Main.java

/**
 * //from w  w  w.j  a  va  2s .  c  o m
 * @param currentNode
 * @param tagName
 * @param attributeValue
 * @return
 */
public static String getTextContentByElementNameANDAttributeValue(Node currentNode, String tagName,
        String attributeValue) {
    String result = "";

    NodeList childNodeList = currentNode.getChildNodes();
    for (int i = 0; i < childNodeList.getLength(); i++) {
        Node childNode = childNodeList.item(i);

        switch (childNode.getNodeType()) {
        case Node.DOCUMENT_NODE:
            break;
        case Node.ELEMENT_NODE:
            Element childElement = (Element) childNodeList.item(i);
            // logger.debug("childElement name : " + childElement.getTagName());
            if (childElement != null && childElement.getNodeName().equals(tagName)) {
                NamedNodeMap attributes = childElement.getAttributes();
                for (int j = 0; j < attributes.getLength(); j++) {
                    Node current = attributes.item(j);

                    if (current.getNodeName().equals("type") && current.getNodeValue().equals(attributeValue)) {
                        result = childElement.getTextContent();
                        break;
                    }
                }
            }
        case Node.TEXT_NODE:
            // logger.debug("textElement name : " + currentNode.getNodeValue());
            break;
        case Node.COMMENT_NODE:
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            break;
        case Node.ENTITY_REFERENCE_NODE:
            break;
        case Node.DOCUMENT_TYPE_NODE:
            break;
        }
    }

    return result;
}

From source file:Main.java

public static Object xmlToBean(Node beanNode) throws ClassNotFoundException, IllegalAccessException,
        InstantiationException, NoSuchMethodException, InvocationTargetException {
    String className = beanNode.getNodeName();
    System.out.println(className);
    Class clazz = Class.forName(className);
    Object bean = clazz.newInstance();
    NodeList fieldNodeList = beanNode.getChildNodes();
    for (int i = 0; i < fieldNodeList.getLength(); i++) {
        Node fieldNode = fieldNodeList.item(i);
        if (fieldNode.getNodeType() == Node.ELEMENT_NODE) {
            String fieldName = fieldNode.getNodeName();
            if (!fieldName.contains(".")) {
                String getName = analyzeMethodName(fieldName, "get");
                String setName = analyzeMethodName(fieldName, "set");
                System.out.println(setName);
                clazz.getMethod(setName, clazz.getMethod(getName).getReturnType()).invoke(bean,
                        fieldNode.getTextContent());
            }/*w ww  .  ja  v a 2 s .  com*/
        }
    }
    System.out.println(bean);
    return bean;
}

From source file:Main.java

public static String getValueXPath(String srcXmlString, String xPath) {
    String value = null;/*from ww  w  . ja v a 2 s. c om*/
    try {
        Object result = execXpathGetNode(srcXmlString, xPath);
        Node node = (Node) result;
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            value = node.getTextContent();
        } else {
            value = node.getNodeValue();
        }
        logger.debug(xPath + " = " + value);
    } catch (Exception ex) {
        logger.error(ex.getMessage() + " Could not extract any value using xpath: " + xPath);
    }
    return value;
}

From source file:Main.java

/**
 * Get the first found child element with the provided name that is a direct
 * child the provided element./*  w  w w .  j  ava 2 s .  c o  m*/
 * 
 * @param parent
 *            The parent element
 * @param name
 *            The name of the child element to find
 * @return The first found child element, null if no matching children
 */
public static Element getFirstChildElementByName(Element parent, String name) {
    assertNotNull(parent);
    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.getNodeName().equals(name)) {
            return (Element) node;
        }
    }

    return null;
}

From source file:Main.java

public static Element getElement(Element parentElement, String nodeName, String id) {
    NodeList nodeList = parentElement.getElementsByTagName(nodeName);
    int i;/* w  ww.  j  a v  a2s  .c  om*/
    for (i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            if (id.equals(element.getAttribute(idName))) {
                return element;
            }
        }
    }
    return null;
}

From source file:Main.java

private static Node getSingleNodeElementByTagName(String tagName, int indexZeroBased) throws Exception {
    NodeList list = getNodeList(tagName);
    Node node = null;//from   ww w. j  a  v  a2 s  .  co m

    if (list.getLength() > 0 && list.item(indexZeroBased).getNodeType() == Node.ELEMENT_NODE) {
        node = list.item(indexZeroBased);
    } else {
        throw new Exception("Xpath Query did not result in any Node elements. Check your Xpath expression");
    }

    return node;
}

From source file:Main.java

public static boolean isNodeTypeElement(Node node) {
    return node != null && node.getNodeType() == Node.ELEMENT_NODE;
}

From source file:Main.java

public static Element firstChildElement(Node node) {
    for (Node tempNode = node.getFirstChild(); tempNode != null; tempNode = tempNode.getNextSibling()) {
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) tempNode;
        }/*from  w ww . j  a va2 s  .  c om*/
    }
    return null;
}

From source file:Main.java

public static List<Element> getChildElementsByTagName(Element element, String name) {

    List<Element> elements = new LinkedList<Element>();

    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element e = (Element) node;
            if (e.getTagName().equals(name)) {
                elements.add(e);//from  w  w w.j  a va2 s  .  co  m
            }
        }
    }

    return elements;
}