Example usage for org.w3c.dom Node getNodeType

List of usage examples for org.w3c.dom Node getNodeType

Introduction

In this page you can find the example usage for org.w3c.dom Node getNodeType.

Prototype

public short getNodeType();

Source Link

Document

A code representing the type of the underlying object, as defined above.

Usage

From source file:Main.java

public static String getTextData(Node node) {
    if (!node.hasChildNodes()) {
        return null;
    }/*from   w w  w  .j a v a 2  s  . c o m*/
    Node child = node.getFirstChild();
    while (child != null && child.getNodeType() != Node.TEXT_NODE
            && child.getNodeType() != Node.CDATA_SECTION_NODE) {
        child = child.getNextSibling();
    }
    if (child == null) {
        return null;
    }

    if (child.getNodeType() == Node.TEXT_NODE) {
        return ((Text) child).getData();
    } else {
        return ((CDATASection) child).getData();
    }

}

From source file:Main.java

public static Element getFirstChildElement(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);
        if (node.getNodeType() == Node.ELEMENT_NODE && (name == null || node.getNodeName().equals(name)))
            return (Element) node;
    }//from   w w w .jav a  2 s  .c  o m

    return null;
}

From source file:Main.java

public static Node findSubElement(Node parent, String localName) {
    if (parent == null) {
        return null;
    }/*  ww  w.  ja v a2 s  .c o  m*/
    Node child = parent.getFirstChild();
    while (child != null) {
        if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getLocalName().equals(localName))) {
            return child;
        }
        child = child.getNextSibling();
    }
    return null;
}

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());
            }//ww  w. j av  a 2  s  .  c om
        }
    }
    System.out.println(bean);
    return bean;
}

From source file:Main.java

/**
 * Collapses a list of CDATASection, Text, and predefined EntityReference
 * nodes into a single string.  If the list contains other types of nodes,
 * those other nodes are ignored./*www  .jav a2s .c o m*/
 */
public static String getText(NodeList nodeList) {
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        switch (node.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
        case Node.TEXT_NODE:
            buffer.append(node.getNodeValue());
            break;
        case Node.ENTITY_REFERENCE_NODE:
            if (node.getNodeName().equals("amp"))
                buffer.append('&');
            else if (node.getNodeName().equals("lt"))
                buffer.append('<');
            else if (node.getNodeName().equals("gt"))
                buffer.append('>');
            else if (node.getNodeName().equals("apos"))
                buffer.append('\'');
            else if (node.getNodeName().equals("quot"))
                buffer.append('"');
            // Any other entity references are ignored
            break;
        default:
            // All other nodes are ignored
        }
    }
    return buffer.toString();
}

From source file:Main.java

/** Get the next sibling with the same name and type
 *//* ww  w. ja va  2 s . co  m*/
public static Node getNext(Node current) {
    String name = current.getNodeName();
    int type = current.getNodeType();
    return getNext(current, name, type);
}

From source file:Main.java

/**
 * Gets the first child Element of the node, skipping any Text nodes such as whitespace.
 * /*from w  ww. j  a v  a 2  s.  c o m*/
 * @param n The parent in which to search for children
 * @return The first child Element of n, or null if none
 */
public static Element getFirstChildElement(Node n) {
    Node child = n.getFirstChild();
    while (child != null && child.getNodeType() != Node.ELEMENT_NODE) {
        child = child.getNextSibling();
    }

    if (child != null) {
        return (Element) child;
    } else {
        return null;
    }
}

From source file:Main.java

/***************************************************************************
 * Searches nodes in the given list for a child with a given name and value.
 * //  w  w  w  . j a v a2  s .  c o  m
 * @param list
 * @param childName
 * @param value
 * @return
 * @throws Exception
 **************************************************************************/
public static Node searchNodes(NodeList list, String childName, String value) throws Exception {
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        if (value.equals(getChildValueByName((Element) node, childName)))
            return node;
    }

    return null;
}

From source file:Main.java

private static String getTextContent(final Node node) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
    case Node.ATTRIBUTE_NODE:
    case Node.ENTITY_NODE:
    case Node.ENTITY_REFERENCE_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
        return mergeTextContent(node.getChildNodes());
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
    case Node.COMMENT_NODE:
    case Node.PROCESSING_INSTRUCTION_NODE:
        return node.getNodeValue();
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_TYPE_NODE:
    case Node.NOTATION_NODE:
    default://from  w w w  .j av  a2s .co  m
        return null;
    }
}

From source file:Main.java

/**
 * Helper method to get a list of only {@link Text} and {@link Element} typed {@link Node}s.
 * This is partially to workaround the difficulty of working with the {@link NodeList} object.
 * /* w ww  . j  a  v  a 2s  .  co m*/
 * @param node
 *            the node whose children to get
 * 
 * @return the filtered list of child nodes
 */
private static List<Node> getChildNodes(Node node) {
    List<Node> children = new ArrayList<Node>();
    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node child = nl.item(i);
        short type = child.getNodeType();
        if (type == Node.ELEMENT_NODE) {
            children.add(child);
        } else if (type == Node.TEXT_NODE) {
            String text = ((Text) child).getTextContent().trim();
            if (text.length() > 0) {
                children.add(child);
            }
        }
    }
    return children;
}