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

/**
 * Method getFullTextChildrenFromElement
 *
 * @param element//from   w w  w .  j  ava2 s .c o  m
 * @return the string of chi;ds
 */
public static String getFullTextChildrenFromElement(Element element) {

    StringBuffer sb = new StringBuffer();
    NodeList children = element.getChildNodes();
    int iMax = children.getLength();

    for (int i = 0; i < iMax; i++) {
        Node curr = children.item(i);

        if (curr.getNodeType() == Node.TEXT_NODE) {
            sb.append(((Text) curr).getData());
        }
    }

    return sb.toString();
}

From source file:Main.java

public static List<Node> getChildElements(Node node) {
    NodeList children = node.getChildNodes();
    int childCount = children.getLength();
    List<Node> nodes = new ArrayList<Node>(childCount);
    for (int i = 0; i < childCount; i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            nodes.add(child);/*  w w w  .ja va2  s . c o m*/
        }
    }
    return nodes;
}

From source file:Main.java

public static void getAllChildrenText(Element paramElement, String paramString,
        AbstractList paramAbstractList) {
    NodeList localNodeList = paramElement.getChildNodes();
    for (int i = 0; i < localNodeList.getLength(); i++) {
        Node localNode = localNodeList.item(i);
        if ((localNode.getNodeType() == 1) && (((Element) localNode).getTagName().equals(paramString))) {
            String str = getNodeText(localNode);
            if (str != null)
                paramAbstractList.add(str);
        }/*from  www  .j  a  v  a2 s . c  o m*/
    }
}

From source file:Main.java

public static String getChildNodeValue(String nodeName, Node parent) {

    if (parent == null) {
        return null;
    }/*w ww.j  a  v  a  2 s.  co  m*/

    NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equals(nodeName)) {
                Node firstChild = node.getFirstChild();
                if ((firstChild != null) && (firstChild.getNodeType() == Node.TEXT_NODE)) {
                    return firstChild.getNodeValue();
                } else {
                    return null;
                }
            }
        }
    }

    return null;
}

From source file:Main.java

public static Element NodeToElement(Node node) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) node;
    }//from w w  w . java  2 s.  com
    return null;
}

From source file:Main.java

/**
 * Gets XML elements//from   ww w  .j a va 2 s  .c  om
 * 
 * @param elementTag
 * @param fromElement
 * @return
 */
public static List getElements(String elementTag, Element fromElement) {
    List elements = new ArrayList();
    NodeList nodeList = fromElement.getElementsByTagName(elementTag);
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            elements.add((Element) node);
        }
    }
    return elements;
}

From source file:Main.java

public static String getTextContent(Element element) {
    StringBuilder buffer = new StringBuilder();

    element.normalize();//from  w w  w.j a  va 2 s.  com
    NodeList children = element.getChildNodes();
    for (int i = 0, n = children.getLength(); i < n; i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            buffer.append(child.getNodeValue());
        }
    }

    return buffer.toString().trim();
}

From source file:Main.java

public static Object getContent(Element element) {
    NodeList nl = element.getChildNodes();
    StringBuffer content = new StringBuffer();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            return node;
        case Node.CDATA_SECTION_NODE:
        case Node.TEXT_NODE:
            content.append(node.getNodeValue());
            break;
        }/*from w w w.j  a  va 2s.  co m*/
    }
    return content.toString().trim();
}

From source file:Main.java

/**
 * Gets the nodenames of childs./*from  www .j a va2 s. c o m*/
 *
 * @param dataNode
 *            the data node
 * @return the nodenames of childs
 */
public static List<String> getNodenamesOfChilds(Node dataNode) {
    List<String> returnList = new ArrayList<String>();

    NodeList list = dataNode.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() != Node.TEXT_NODE) {
            returnList.add(node.getNodeName());
        }
    }

    return returnList;
}

From source file:Main.java

/**
 * Set the text content of an element. All exisitng Text Node are
 * removed before adding a new Text Node containing the given text.
 *
 * @param element target element to set text content, cannot be null.
 * @param text content of the element, cannot be null.
 *//*from   w w  w .j ava2 s . c  om*/
public static void setElementText(Element element, String text) {

    // Remove all text element
    NodeList list = element.getChildNodes();
    int len = list.getLength();

    for (int i = 0; i < len; i++) {
        Node n = list.item(i);

        if (n.getNodeType() == Node.TEXT_NODE) {
            element.removeChild(n);
        }
    }
    Node child = element.getFirstChild();
    Node textnode = element.getOwnerDocument().createTextNode(text);

    // insert text node as first child
    if (child == null) {
        element.appendChild(textnode);
    } else {
        element.insertBefore(textnode, child);
    }
}