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

/**prints the document tree
 * @param node node to start at/*from w  w  w . j av a 2  s.c om*/
 * @param ident amount of indention*/
public static void printTree(Node node, int ident) {
    if (node == null)
        return;
    NodeList children;
    System.out.print("Node: " + node.getNodeName() + " ");
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        System.out.println("Document Node");
        break;

    case Node.ELEMENT_NODE:
        System.out.println("Element Node");
        break;

    case Node.TEXT_NODE:
        System.out.println("->" + node.getNodeValue().trim() + "<-");
        break;

    case Node.CDATA_SECTION_NODE:
        System.out.println("CData Node");
        break;

    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println("Proposing Instruction Node");
        break;

    case Node.ENTITY_REFERENCE_NODE:
        System.out.println("Entity Node");
        break;

    case Node.DOCUMENT_TYPE_NODE:
        System.out.println("Document Node");
        break;

    default:
    }

    for (int j = 0; j < 2 * ident; j++)
        System.out.print(" ");
    System.out.println("It has the following Children");
    children = node.getChildNodes();
    if (children != null) {
        for (int i = 0; i < children.getLength(); i++) {
            for (int j = 0; j < ident; j++)
                System.out.print(" ");
            System.out.print("Child " + ident + "." + i + " = ");
            printNodeType(children.item(i), ident + 1);
        }
        System.out.println();
    }
}

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  a v  a  2s.  c o m*/
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);
    }
}

From source file:Main.java

public static String getElementText(Element ele) {

    // is there anything to do?
    if (ele == null) {
        return null;
    }/* w  w w.  j  a  va2 s  .  c o m*/
    // get children text
    Node child = ele.getFirstChild();
    if (child != null) {
        short type = child.getNodeType();
        if (type == Node.TEXT_NODE) {
            return child.getNodeValue();
        }
    }
    // return text value
    return null;
}

From source file:Main.java

public static String convertDOMToString(Node node) {
    StringBuffer sb = new StringBuffer();
    if (node.getNodeType() == Node.TEXT_NODE) {
        sb.append(node.getNodeValue());/*from  w w w .  j a  v a2 s. co m*/
    } else {
        String currentTag = node.getNodeName();
        sb.append('<');
        sb.append(currentTag);
        appendAttributes(node, sb);
        sb.append('>');
        if (node.getNodeValue() != null) {
            sb.append(node.getNodeValue());
        }

        appendChildren(node, sb);

        appendEndTag(sb, currentTag);
    }
    return sb.toString();
}

From source file:Main.java

/**
 * //ww  w  .ja  v a2 s .  co  m
 * @return one large string with the contents of all TextNodes, or null if
 *         there are non text nodes or no text nodes as children.
 */
public static String getContentsOfTextOnlyNode(Node n) {
    NodeList children = n.getChildNodes();
    if (children.getLength() == 0) {
        return null;
    }

    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node.getNodeType() == Node.TEXT_NODE) {
            sb.append(node.getNodeValue());
        } else {
            return null;
        }
    }

    return sb.toString();
}

From source file:Main.java

public static String getElementTextNodeValue(Node root, String nodeString) {
    Element elem = getElementNode(root, nodeString);
    if (elem == null) {
        return null;
    }//from w  ww .  jav a  2  s  .  com
    NodeList nlist = elem.getChildNodes();

    if (nlist == null) {
        return null;
    }
    for (int i = 0; i < nlist.getLength(); i++) {
        if (nlist.item(i).getNodeType() == Node.TEXT_NODE) {
            return nlist.item(i).getNodeValue();
        }
    }

    return null;
}

From source file:Main.java

/**
 * Returns all child character data of the given element, including CDATA sections but NOT entity references.
 * @param parentEl the parent element.//from   w w  w .  j  a v  a2  s.c  om
 * @return the child character data of the element, or null if the element is null.
 */
static public 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;
        //            case Node.ENTITY_REFERENCE_NODE : strBuf.append("&").append(tempNode.getNodeName()).append(";");

        }
        tempNode = tempNode.getNextSibling();
    }
    return strBuf.toString();
}

From source file:Main.java

/**
 * Method getFullTextChildrenFromElement
 *
 * @param element//  www  .  j a va2 s  .com
 * @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:Util.java

public static String getTextValue(Node node) {
    StringBuffer textValue = new StringBuffer();
    int length = node.getChildNodes().getLength();
    for (int i = 0; i < length; i++) {
        Node c = node.getChildNodes().item(i);
        if (c.getNodeType() == Node.TEXT_NODE) {
            textValue.append(c.getNodeValue());
        }//from   ww w. j  a  va  2s  .  c  o  m
    }
    return textValue.toString().trim();
}

From source file:Main.java

public static ArrayList getChildTextNodeValues(Node theNode) {
    ArrayList arrayList = new ArrayList();
    if (theNode.getNodeType() != Node.TEXT_NODE) {
        Element theNodeElement = (Element) theNode;
        NodeList theNodeList = theNodeElement.getChildNodes();
        for (int i = 0; i < theNodeList.getLength(); i++) {
            Node node = getTextNode(theNodeList.item(0));
            if (node != null) {
                String s = node.getNodeValue();
                arrayList.add(s);//ww  w . j a v a 2  s .c o m
            }
        }
    }
    return arrayList;
}