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

/**
 * Get the content of the given element.
 * @param element       The element to get the content for.
 * @param defaultStr    The default to return when there is no content.
 * @return              The content of the element or the default.
 *///from   w  w  w  .  jav  a  2 s. c o m
public static String getElementContent(Element element, String defaultStr) {
    if (element == null)
        return defaultStr;
    NodeList children = element.getChildNodes();
    String result = "";
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i).getNodeType() == Node.TEXT_NODE
                || children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) {
            result += children.item(i).getNodeValue();
        }
    }
    return result.trim();
}

From source file:Main.java

/**
 * Gets the String value of the node. //from ww w.  ja v  a2 s . c  o m
 If the node does not contain text then an empty String is returned
 * @param node the node of interest
 * @return the value of that node
 */
public static String getTextForNode(Node node) {
    NodeList children = node.getChildNodes();
    if (children == null) {
        return "";
    }

    for (int i = 0; i < children.getLength(); i++) {
        Node childNode = children.item(i);
        if ((childNode.getNodeType() == Node.TEXT_NODE)
                || (childNode.getNodeType() == Node.CDATA_SECTION_NODE)) {
            return childNode.getNodeValue();
        }
    }

    return "";
}

From source file:Main.java

/**
 * Given an node, returns the child text node of this element.
 * //from  w  w w.  ja  v a  2s .c om
 * @param node
 *            the node to get the text node from
 * @return the text node that is a child of this node, or <CODE>null</CODE>
 *         if there is no such child
 */
public static String containedText(Node node) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node c = children.item(i);
        if (c.getNodeType() != Node.TEXT_NODE)
            continue;
        return ((Text) c).getData();
    }
    return null;
}

From source file:Main.java

public static Element removeElementContentWhitespace(Element root) {
    boolean foundElt = false;
    for (Node n1 = root.getFirstChild(); n1 != null; n1 = n1.getNextSibling()) {
        if (n1.getNodeType() != Node.ELEMENT_NODE)
            continue;
        foundElt = true;/*w  w w . ja  va  2  s  . c o  m*/
        removeElementContentWhitespace(Element.class.cast(n1));
    }
    if (foundElt) {
        Node n1 = root.getFirstChild();
        while (n1 != null) {
            Node n2 = n1.getNextSibling();
            if (n1.getNodeType() == Node.TEXT_NODE && isEmptyText(Text.class.cast(n1))) {
                root.removeChild(n1);
            }
            n1 = n2;
        }
    }
    return root;
}

From source file:Main.java

/**gets string associated with a node
 * @param node node to get string for//from www  .  ja  va2s . c o m
 * @param string associated with the input node*/
static public String getAssociatedString(Node node) {
    // Has to be a real Node
    if (node == null)
        return null;

    // Must have one and only one string associted with it
    NodeList children = node.getChildNodes();
    if (children.getLength() > 1) {
        System.out.println("XMLUtils: Node has more than one child");
        return null;
    }
    Node firstChild = children.item(0);
    if (firstChild.getNodeType() != Node.TEXT_NODE) {
        System.out.println("XMLUtils: Node is not a text node");
        System.out.println(
                "XMLUtils: Node = " + firstChild.getNodeName() + ", Parent Node = " + node.getNodeName());
        return null;
    }
    String stringToReturn = firstChild.getNodeValue().trim();
    if (stringToReturn.equals("")) {
        System.out.println("XMLUtils: Trimmed string is empty");
        return null;
    } else
        return stringToReturn;
}

From source file:Main.java

public static String getTextChild(Element element) {
    NodeList children = element.getChildNodes();
    String value = null;//w  w  w. j a  va2s.c o  m
    for (int i = 0; i < children.getLength(); ++i) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            if (null != value) {
                throw new IllegalStateException(
                        "Element " + elementToString(element) + " has more than one text child.");
            } else {
                value = child.getNodeValue();
            }
        }
    }
    return value;
}

From source file:Main.java

/**
 * Get the text content of an element. If the element contains
 * mixed content (both elements and text), only the first text section
 * is returned.//from w w  w.  j av  a  2 s  .  co  m
 *
 * @param element target element to retrieve text on, cannot be null.
 * @return text content of the element.
 */
public static String getElementText(Element element) {
    element.normalize();
    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) {
            String s = n.getNodeValue();

            if (s != null) {
                return s.trim();
            }
        }
    }
    return null;
}

From source file:Main.java

public static String getTextTag(Node node) {
    Node child = node.getFirstChild();
    if (child.getNodeType() == Node.TEXT_NODE)
        return child.getNodeValue();
    return null;/*from www  . j a  v  a2  s  .  c  o m*/
}

From source file:Main.java

public static Node cloneNode(Document d, Node n) {
    Node r = null;/*from ww w . j  a  v a  2s.  c  om*/
    switch (n.getNodeType()) {
    case Node.TEXT_NODE:
        r = d.createTextNode(((Text) n).getData());
        break;
    case Node.CDATA_SECTION_NODE:
        r = d.createCDATASection(((CDATASection) n).getData());
        break;
    case Node.ELEMENT_NODE:
        r = d.createElement(((Element) n).getTagName());
        NamedNodeMap map = n.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            ((Element) r).setAttribute(((Attr) map.item(i)).getName(), ((Attr) map.item(i)).getValue());
        }
        break;
    }
    return r;
}

From source file:Main.java

/**
 * @param sibling/*from w ww .  j a  v a 2  s .com*/
 * @param uri
 * @param nodeName
 * @param number
 * @return nodes with the constrain
 */
public static Text selectNodeText(Node sibling, String uri, String nodeName, int number) {
    Node n = selectNode(sibling, uri, nodeName, number);
    if (n == null) {
        return null;
    }
    n = n.getFirstChild();
    while (n != null && n.getNodeType() != Node.TEXT_NODE) {
        n = n.getNextSibling();
    }
    return (Text) n;
}