Example usage for org.w3c.dom Node TEXT_NODE

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

Introduction

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

Prototype

short TEXT_NODE

To view the source code for org.w3c.dom Node TEXT_NODE.

Click Source Link

Document

The node is a Text node.

Usage

From source file:Main.java

/**
 * This method returns node value for given child. If there is no text
 * available for given node, then this method returns null
 * /*  ww  w.  j av  a 2  s. c o  m*/
 * @return Node value of input node
 * @throws IllegalArgumentException
 *             if input is invalid
 */
public static String getNodeValue(final Node inputNode) {
    // Child count
    int childCount = 0;

    if (inputNode == null) {
        return null;
    }

    // Return null if child not found
    final NodeList childList = inputNode.getChildNodes();
    if ((childList == null) || (childList.getLength() < 1)) {
        return null;
    }

    // Get child count
    childCount = childList.getLength();

    // For each child
    for (int childIndex = 0; childIndex < childCount; childIndex++) {
        // Get each child
        final Node childNode = childList.item(childIndex);

        // Check if text node
        if (childNode.getNodeType() == Node.TEXT_NODE) {
            // Return node value
            return childNode.getNodeValue();
        }
    }

    // If no text node found return null
    return null;
}

From source file:Main.java

/**
 * Copy one node to another node./*from  www.  j  a  v a  2 s.  co  m*/
 * @param source source Node
 * @param dest destination Node
 * @return destination Node
 */
public static synchronized Node copyNode(Node source, Node dest) {
    if (source.getNodeType() == Node.TEXT_NODE) {
        Text tn = dest.getOwnerDocument().createTextNode(source.getNodeValue());
        return tn;
    }

    Node attr = null;
    NamedNodeMap attrs = source.getAttributes();

    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            attr = attrs.item(i);
            ((Element) dest).setAttribute(attr.getNodeName(), attr.getNodeValue());
        }
    }

    Node child = null;
    NodeList list = source.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        child = list.item(i);
        if (!(child instanceof Text)) {
            Element en = dest.getOwnerDocument().createElementNS(child.getNamespaceURI(), child.getNodeName());

            if (child.getNodeValue() != null) {
                en.setNodeValue(child.getNodeValue());
            }

            Node n = copyNode(child, en);
            dest.appendChild(n);
        } else if (child instanceof CDATASection) {
            CDATASection cd = dest.getOwnerDocument().createCDATASection(child.getNodeValue());
            dest.appendChild(cd);
        } else {
            Text tn = dest.getOwnerDocument().createTextNode(child.getNodeValue());
            dest.appendChild(tn);
        }
    }
    return dest;
}

From source file:Main.java

/**
 * Removes text nodes that are only containing whitespace characters
 * inside a DOM tree.//w  w w . j a  v  a2  s .co  m
 *
 * @param element the root node to normalize.
 */
public static void stripWhitespaceNodes(Node element) {
    Node node, child;
    for (child = element.getFirstChild(); child != null; child = node) {
        node = child.getNextSibling();
        stripWhitespaceNodes(child);
    }

    if (element.getNodeType() == Node.TEXT_NODE && element.getNodeValue().trim().length() == 0) {
        element.getParentNode().removeChild(element);
    }
}

From source file:Main.java

private static String getValue(Node node, short nodeType) {
    switch (nodeType) {
    case Node.ELEMENT_NODE:
        return ((Element) node).getTagName();

    case Node.TEXT_NODE:
        return ((Text) node).getData();

    case Node.PROCESSING_INSTRUCTION_NODE:
        return ((ProcessingInstruction) node).getData();

    default:/*from   w  w  w .  j  ava  2s . c  om*/
        return "";
    }
}

From source file:Main.java

/**
 * Returns the node type name from the node type code
 *
 * @param nodeType//from   w ww  . j  a v a 2s  .com
 * @return
 */
public static String getTypeName(short nodeType) {
    switch (nodeType) {
    case Node.ELEMENT_NODE:
        return "element";
    case Node.DOCUMENT_NODE:
        return "document";
    case Node.TEXT_NODE:
        return "text";
    case Node.ATTRIBUTE_NODE:
        return "attribute";
    case Node.CDATA_SECTION_NODE:
        return "cdata";
    }
    return "Unknown[" + nodeType + "]";
}

From source file:Main.java

/**
 * We get a object that connected text node of the child node and the CDATA section as character string.
 * @param node//from w  ww.  j a va  2  s  .co  m
 * @return
 */
public static String getChildText(Node node) {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < node.getChildNodes().getLength(); i++) {
        Node n = node.getChildNodes().item(i);
        if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE) {
            buf.append(n.getNodeValue());
        }
    }
    return buf.toString();
}

From source file:Main.java

public static String getElementText(Element element) {
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            return ((Text) child).getNodeValue();
        }/*from   ww w. j  a  v  a 2  s. c om*/
    }
    return "";
}

From source file:Main.java

/**
 * Extracts the inner text value from a xml node accept : <node>value</node> and return value
 * string/*w  ww  . jav a  2  s.c o m*/
 * 
 * @param node XML node to extract inner text from
 * @return innerText if its not empty otherwise null.
 */
private static String getNodeTextValue(Node node) {
    String innerText = null;
    if (node != null) {
        Node textNode = node.getFirstChild();
        if (textNode != null && textNode.getNodeType() == Node.TEXT_NODE) {
            innerText = textNode.getNodeValue();
            innerText = innerText.trim();
            if (innerText.length() == 0) {
                innerText = null;
            }
        }
    }
    return innerText;
}

From source file:Main.java

public static final String getCDATA(Element elem, boolean trim) {
    StringBuffer sb = new StringBuffer();
    NodeList nl = elem.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nc = nl.item(i);/*  w ww .j  a  v a 2 s  . c o  m*/
        if (nc.getNodeType() == Node.CDATA_SECTION_NODE) {
            sb.append(((Text) nc).getData());
        } else if (nc.getNodeType() == Node.TEXT_NODE) {
            String txt = ((Text) nc).getData();
            if (trim) {
                txt = txt.trim();
            }
            sb.append(txt);
        }
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Method getStrFromNode/*ww w.ja  va 2 s. c  o m*/
 *
 * @param xpathnode
 * @return the string for the node.
 */
public static String getStrFromNode(Node xpathnode) {
    if (xpathnode.getNodeType() == Node.TEXT_NODE) {
        // we iterate over all siblings of the context node because eventually,
        // the text is "polluted" with pi's or comments
        StringBuilder sb = new StringBuilder();

        for (Node currentSibling = xpathnode.getParentNode()
                .getFirstChild(); currentSibling != null; currentSibling = currentSibling.getNextSibling()) {
            if (currentSibling.getNodeType() == Node.TEXT_NODE) {
                sb.append(((Text) currentSibling).getData());
            }
        }

        return sb.toString();
    } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) {
        return ((Attr) xpathnode).getNodeValue();
    } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        return ((ProcessingInstruction) xpathnode).getNodeValue();
    }

    return null;
}