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

/**
 * Hack...since DOM reads newlines as textnodes we want to strip out those nodes to make it easier to use the tree.
 *//*from w ww  .ja v  a  2s.co  m*/
public static void stripEmptyTextNodes(Node n) {
    NodeList children = n.getChildNodes();
    int length = children.getLength();
    for (int i = 0; i < length; i++) {
        Node c = children.item(i);
        if (!c.hasChildNodes() && c.getNodeType() == Node.TEXT_NODE
                && c.getTextContent().trim().length() == 0) {
            n.removeChild(c);
            i--;
            length--;
            children = n.getChildNodes();
        } else {
            stripEmptyTextNodes(c);
        }
    }
}

From source file:Main.java

public static String getTextContent(Element elm) {
    if (elm == null) {
        return null;
    }//from   w  ww  .ja  v  a2  s .  com
    StringBuilder result = new StringBuilder();
    NodeList childNodes = elm.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if ((node == null) || (node.getNodeType() != Node.TEXT_NODE)) {
            continue;
        }
        result.append(((Text) node).getData());
    }
    return result.toString().trim();
}

From source file:Main.java

/**
 * Gets the previous comment.//ww  w  . jav a  2  s .  c  o m
 * 
 * @param element
 *            the element
 * @return the previous comment
 */
public static String getPreviousComment(Node element) {
    while (element.getPreviousSibling() != null) {
        Node prev = element.getPreviousSibling();
        if (prev.getNodeType() == Node.COMMENT_NODE) {
            return prev.getTextContent();
        } else if (prev.getNodeType() == Node.TEXT_NODE) {
            return getPreviousComment(prev);
        } else if (prev.getNodeType() == Node.ELEMENT_NODE) {
            return null;
        }
    }
    return null;
}

From source file:Main.java

/**
 *  Sets the text for a node//from  w ww . jav  a 2 s.c  o  m
 */
public synchronized static void setText(Node n, String text) {
    if (n == null)
        throw new IllegalArgumentException("Node argument cannot be null");
    if (text == null)
        throw new IllegalArgumentException("Node text argument cannot be null");

    NodeList nl = n.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeType() == Node.TEXT_NODE) {
            nl.item(i).setNodeValue(text);
            return;
        }
    }

    Node textNode = n.getOwnerDocument().createTextNode(text);
    n.appendChild(textNode);
}

From source file:Main.java

public static List<Node> getAllChildNodes(Node node) {
    if (node == null)
        return null;
    List<Node> result = new ArrayList<Node>();
    NodeList nodelist = node.getChildNodes();
    for (int i = 0; i < nodelist.getLength(); i++) {
        Node curnode = nodelist.item(i);
        int type = curnode.getNodeType();
        if (type != Node.TEXT_NODE)
            result.add(nodelist.item(i));
        List<Node> childlist = getAllChildNodes(curnode);
        if (childlist != null)
            result.addAll(childlist);//w  w  w.  ja v a  2 s .  c om
    }
    return result;
}

From source file:Main.java

/**
 * @param node/*from  w w  w  . j  a va2s  .  c  om*/
 */
public static void displayNodeInfo(Node node) {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        System.out.println("Document Node ");
        break;
    case Node.ELEMENT_NODE:
        System.out.println("Element Node: " + node.getNodeName());
        break;
    case Node.TEXT_NODE:
        System.out.println("Text Node: " + node.getNodeName());
        break;
    case Node.CDATA_SECTION_NODE:
        System.out.println("CDATA Section Node: ");
        break;
    case Node.COMMENT_NODE:
        System.out.println("Comment Node ");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println("Processing Instruction Node ");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.println("Entity Reference Node ");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        System.out.println("Document Type Node ");
        break;
    }
}

From source file:Main.java

public static String getTextContent(Node e) {
    StringBuilder sb = new StringBuilder();

    NodeList children = e.getChildNodes();
    if (children != null) {
        for (int i = 0; i < children.getLength(); i++) {
            Node n = children.item(i);
            if (n.getNodeType() == Node.TEXT_NODE) {
                sb.append(n.getNodeValue());
            }// w  ww.j a v  a2 s.co m
        }
    }

    return sb.toString();
}

From source file:Main.java

public static String getText(Element e) {
    NodeList nl = e.getChildNodes();
    int max = nl.getLength();
    for (int i = 0; i < max; i++) {
        Node n = nl.item(i);/*  w ww.ja  va2s  .c  o  m*/
        if (n.getNodeType() == Node.TEXT_NODE) {
            return n.getNodeValue();
        }
    }
    return "";
}

From source file:Main.java

/** Takes XML node and prints to String.
 *
 * @param node Element to print to String
 * @return XML node as String/*from  www.j ava  2  s .  c om*/
 */
private static void printNode(StringBuffer sBuffer, Node node) {
    if (node.getNodeType() == Node.TEXT_NODE) {
        sBuffer.append(encodeXMLText(node.getNodeValue().trim()));
    } else if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
        sBuffer.append("<![CDATA[");
        sBuffer.append(node.getNodeValue());
        sBuffer.append("]]>");
    } else if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element el = (Element) node;

        sBuffer.append("<").append(el.getTagName());

        NamedNodeMap attribs = el.getAttributes();

        for (int i = 0; i < attribs.getLength(); i++) {
            Attr nextAtt = (Attr) attribs.item(i);
            sBuffer.append(" ").append(nextAtt.getName()).append("=\"").append(nextAtt.getValue()).append("\"");
        }

        NodeList nodes = node.getChildNodes();

        if (nodes.getLength() == 0) {
            sBuffer.append("/>");
        } else {
            sBuffer.append(">");

            for (int i = 0; i < nodes.getLength(); i++) {
                printNode(sBuffer, nodes.item(i));
            }

            sBuffer.append("</").append(el.getTagName()).append(">");
        }
    }
}

From source file:Main.java

/**
 * Set the value of element/*from  w ww .ja  v  a  2 s . c om*/
 *
 * @param element
 * @param val
 */
public static void setElementValue(Element element, String val) {
    Node node = element.getOwnerDocument().createTextNode(val);
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nd = nl.item(i);
        if (nd.getNodeType() == Node.TEXT_NODE) {
            nd.setNodeValue(val);
            return;
        }
    }
    element.appendChild(node);
}