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

/**
 * Concat all the text and cdata node children of this elem and return
 * the resulting text.//w ww  .  j a va 2s  . c  o  m
 * (by Matt Duftler)
 *
 * @param parentEl the element whose cdata/text node values are to
 *                 be combined.
 * @return the concatanated string.
 */
public static String getChildCharacterData(Element parentEl) {
    if (parentEl == null) {
        return null;
    }
    Node tempNode = parentEl.getFirstChild();
    StringBuffer strBuf = new StringBuffer();
    CharacterData charData;

    while (tempNode != null) {
        switch (tempNode.getNodeType()) {
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
            charData = (CharacterData) tempNode;
            strBuf.append(charData.getData());
            break;
        }
        tempNode = tempNode.getNextSibling();
    }
    return strBuf.toString();
}

From source file:Main.java

/**
 * Returns first of the <tt>element</tt>'s child nodes that is of type
 * <tt>nodeType</tt>./* w  w  w .j a  v  a2  s  . c o  m*/
 * @param element the element whose child we need.
 * @param nodeType the type of the child we need.
 * @return a child of the specified <tt>nodeType</tt> or null if none
 * was found.
 */
public static Node getChildByType(Element element, short nodeType) {
    if (element == null)
        return null;

    NodeList nodes = element.getChildNodes();
    if (nodes == null || nodes.getLength() < 1)
        return null;

    Node node;
    String data;
    for (int i = 0; i < nodes.getLength(); i++) {
        node = nodes.item(i);
        short type = node.getNodeType();
        if (type == nodeType) {
            if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE) {
                data = ((Text) node).getData();
                if (data == null || data.trim().length() < 1)
                    continue;
            }

            return node;
        }
    }

    return null;
}

From source file:Main.java

/**
 * reads all subsequent text nodes and returns the combined string needed
 * because escape sequences are parsed into consecutive text nodes e.g.
 * "abc&amp;123" --> (abc)(&)(123)
 **//*from   w w w.  j  a v  a2 s.  c  o  m*/
private static String getXMLText(NodeList nl, int i, boolean trim) {
    StringBuffer strBuff = null;

    String text = nl.item(i).getTextContent();
    if (text == null)
        return null;

    for (i++; i < nl.getLength() && nl.item(i).getNodeType() == Node.TEXT_NODE; i++) {
        if (strBuff == null)
            strBuff = new StringBuffer(text);

        strBuff.append(nl.item(i).getTextContent());
    }
    if (strBuff != null)
        text = strBuff.toString();

    if (trim)
        text = text.trim();

    return text;
}

From source file:Main.java

/**
 * Gets a TEXT node value from the specified Element node.
 *
 * @param element a ELEMENT Node has TEXT node 
 */// ww  w  .j a v a2s.c o  m
/* TODO: Child? */
public static String getTextData(Node element) {
    if (element.getNodeType() != Node.ELEMENT_NODE) {
        throw new IllegalArgumentException(element + " is not ELEMENT node");
    }
    Node description = element.getNextSibling();
    if (description.getNodeType() != Node.TEXT_NODE) {
        throw new IllegalArgumentException(description + " is not TEXT node");
    }
    return description.getNodeValue();
}

From source file:Main.java

private static void extractText(Node n, StringBuffer buf) {
    if (n.getNodeType() == Node.TEXT_NODE) {
        buf.append(n.getNodeValue());//w  ww . ja va  2 s.  c o  m
    }

    for (n = n.getFirstChild(); n != null; n = n.getNextSibling()) {
        extractText(n, buf);
    }
}

From source file:XMLUtils.java

/**
 * Extract all text children of an element
 *//*from   w w  w. ja  v  a  2s  .  com*/
public static String extractTextChildren(Element parentNode) {
    NodeList childNodes = parentNode.getChildNodes();
    String result = new String();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.TEXT_NODE) {
            result += node.getNodeValue();
        }
    }
    return result;
}

From source file:Main.java

/**
 * Get the trimmed text of a text (simple content) element.
 * /*from  ww  w.  j a  v  a 2 s. c  o  m*/
 * @param e The element.
 * @return The text of the specified element, null if it has no text nodes.
 */
public static String getText(Element e) {
    if (e == null)
        return null;
    NodeList lst = e.getChildNodes();
    int size = lst.getLength();
    for (int i = 0; i < size; i++) {
        Node n = lst.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            Text t = (Text) n;
            String s = t.getData();
            return s == null ? null : s.trim();
        }
    }
    return null;
}

From source file:Main.java

public static String getNodeValue(Node node) {
    NodeList childNodes = node.getChildNodes();
    for (int x = 0; x < childNodes.getLength(); x++) {
        Node data = childNodes.item(x);
        if (data.getNodeType() == Node.TEXT_NODE)
            return data.getNodeValue();
    }//w w w  .j  av  a 2s .c  o  m
    return "";
}

From source file:Main.java

public static Node getFirstTextNode(Node node) {
    NodeList nl = ((Element) node).getChildNodes();

    Node ret = null;//from  w w w .java 2  s.  co  m
    int i = 0;
    while (ret == null && i < nl.getLength()) {
        Node n = nl.item(i++);
        if (n.getNodeType() == Node.TEXT_NODE)
            ret = n;
    }

    return ret;
}

From source file:Main.java

/**
 * Prints information of the specified node.
 * /*  w w  w . ja  va2s.  c  om*/
 * @param node the specified node.
 * @param level a number of indent.
 */
public static void printNode(Node node, int level) {
    if (node == null)
        return;
    switch (node.getNodeType()) {
    case Node.ATTRIBUTE_NODE:
        printAttribute(node, level);
        break;
    case Node.TEXT_NODE:
        printTextNode(node, level);
        break;
    default:
        printElement(node, level);
    }
}