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 value of an element. If the node is a Document, use the
 * document element as the element node.
 * The value of an element node is the sum of all the element's
 * first generation child text nodes. Note that this is not what you
 * would get from a mixed element in an XSL program.
 * @param node the node.//  www .j  a v a  2  s .  co  m
 * @return the value of the element, or an empty string if the
 * node is not an element.
 */
public static String getElementValue(Node node) {
    if (node instanceof Document)
        node = ((Document) node).getDocumentElement();
    if (!(node instanceof Element))
        return "";
    NodeList nodeList = node.getChildNodes();
    String value = "";
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node n = nodeList.item(i);
        if (n.getNodeType() == Node.TEXT_NODE)
            value += n.getNodeValue();
    }
    return value;
}

From source file:Main.java

public static String logElement(final Element elem, final int level) {

    String estr = "";
    String indentText = "  ";
    String addIndT = "";

    // add indent
    int ind = 0;//from w  w  w  .j a  va2 s.  co m
    while (ind < level) {
        addIndT = addIndT + indentText;
        ind++;

    }
    String name = elem.getNodeName();
    estr = "\n" + addIndT + "<" + name + " ";
    // Attribs
    NamedNodeMap namedNodeMap = elem.getAttributes();
    StringBuilder sb = new StringBuilder(estr);
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Attr att = (Attr) namedNodeMap.item(i);
        sb.append(" " + estr + att.getName() + "=\"" + att.getNodeValue() + "\" ");
    }
    sb.append(">");
    estr = sb.toString();
    NodeList pl = elem.getChildNodes();
    int index = pl.getLength();
    // text nodes
    if (index > 0) {
        int i = 0;
        while (i < index) {
            Node domNode = pl.item(i);
            if ((domNode.getNodeType()) == org.w3c.dom.Node.TEXT_NODE) {
                String Etext = domNode.getNodeValue();
                estr = estr + "\n " + addIndT + addIndT + Etext;
            }
            i++;
        }
    }
    // Child Elements
    if (index > 0) {
        int i = 0;
        while (i < index) {
            Node domNode = pl.item(i);
            if ((domNode.getNodeType()) == org.w3c.dom.Node.ELEMENT_NODE) {
                Element el = (Element) domNode;
                estr = estr + logElement(el, level + 1);
            }
            i++;
        }
    }
    estr = estr + "\n" + addIndT + "</" + name + ">";

    return estr;
}

From source file:Main.java

/**
 * Get XML <code>Node</code> text content.  This method duplicates the
 * org.w3c.dom.Node.getTextContent() method in JDK 1.5.
 *
 * @param baseNode The XML node from which the content is being retrieved.
 * @return The text content of the XML node.
 *///  ww w . j  a v a2s .c  om
public static String getTextContent(Node baseNode) {
    // if element, first child will be a text element with content
    Node child = baseNode.getFirstChild();
    if (child != null && child.getNodeType() == Node.TEXT_NODE) {
        return child.getNodeValue();
    }
    return "";
}

From source file:Main.java

public static void visitRecursively(Node node, BufferedWriter bw) throws Exception {
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        // get child node
        Node childNode = list.item(i);
        if (childNode.getNodeType() == Node.TEXT_NODE) {
            System.out.println("Found Node: " + childNode.getNodeName() + " - with value: "
                    + childNode.getNodeValue() + " Node type:" + childNode.getNodeType());

            String nodeValue = childNode.getNodeValue();
            nodeValue = nodeValue.replace("\n", "").replaceAll("\\s", "");
            if (!nodeValue.isEmpty()) {
                System.out.println(nodeValue);
                bw.write(nodeValue);/*w  ww.  j a v  a2  s . c  om*/
                bw.newLine();
            }
        }
        visitRecursively(childNode, bw);
    }
}

From source file:Main.java

/**
 * based on public Java5 javadoc of org.w3c.dom.Node.getTextContent method
 *//* w w w  .  ja  v a 2s .  co m*/
public static String getTextContent(Node node) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
    case Node.ATTRIBUTE_NODE:
    case Node.ENTITY_NODE:
    case Node.ENTITY_REFERENCE_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
        return mergeTextContent(node.getChildNodes());
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
    case Node.COMMENT_NODE:
    case Node.PROCESSING_INSTRUCTION_NODE:
        return node.getNodeValue();
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_TYPE_NODE:
    case Node.NOTATION_NODE:
    default:
        return null;
    }
}

From source file:Main.java

/**
 * Checks if the given node has only text children.
 *
 * @param node parent.//w  ww. j  av  a 2s. c  o  m
 * @return 'TRUE' if the given node has only text children; 'FALSE' otherwise.
 */
public static boolean hasOnlyTextChildNodes(final Node node) {
    boolean result = true;
    final NodeList children = node.getChildNodes();
    for (int i = 0; result && i < children.getLength(); i++) {
        final Node child = children.item(i);
        if (child.getNodeType() != Node.TEXT_NODE) {
            result = false;
        }
    }

    return result;
}

From source file:Main.java

/**
 * Get the text that is associated with this element.
 * //from  w ww  . j  ava  2 s .c o m
 * @param element an Element object.
 * @return the text that is associated with this element.
 */
public static String getText(Element element) {
    String text = null;

    // Get first child element
    Node node = element.getFirstChild();

    // NodeList nodeList = element.getChildNodes();

    // int length = nodeList.getLength();

    // Process while there are nodes and the text hasn't been found
    while ((node != null) && (text == null)) {
        // If a text node or cdata section is found, then get text
        if ((node.getNodeType() == Node.TEXT_NODE) || (node.getNodeType() == Node.CDATA_SECTION_NODE)) {
            text = ((CharacterData) node).getData();
        }

        // Get next sibling
        node = node.getNextSibling();
    }

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

    return text;
}

From source file:Main.java

public static String logElement(Element El, int level) {

    String Es = "";
    String indentText = "  ";
    String addIndT = "";

    // add indent
    int ind = 0;//  w  ww .  ja  v a2s .c  o  m
    while (ind < level) {
        addIndT = addIndT + indentText;
        ind++;

    }
    String name = El.getNodeName();
    Es = "\n" + addIndT + "<" + name + " ";
    // Attribs
    NamedNodeMap namedNodeMap = El.getAttributes();
    StringBuilder sb = new StringBuilder(Es);
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Attr att = (Attr) namedNodeMap.item(i);
        sb.append(" " + Es + att.getName() + "=\"" + att.getNodeValue() + "\" ");
        // Es = " " + Es + att.getName() + "=\"" + att.getNodeValue() +
        // "\" ";
    }
    sb.append(">");
    // Es = Es + ">";
    Es = sb.toString();
    NodeList pl = El.getChildNodes();
    int index = pl.getLength();
    // text nodes
    if (index > 0) {
        int i = 0;
        while (i < index) {
            Node DomNode = pl.item(i);
            if ((DomNode.getNodeType()) == org.w3c.dom.Node.TEXT_NODE) {
                String Etext = DomNode.getNodeValue();
                Es = Es + "\n " + addIndT + addIndT + Etext;
            }
            i++;
        }
    }
    // Child Elements
    if (index > 0) {
        level++;
        int i = 0;
        while (i < index) {
            Node DomNode = pl.item(i);
            if ((DomNode.getNodeType()) == org.w3c.dom.Node.ELEMENT_NODE) {
                Element el = (Element) DomNode;
                Es = Es + logElement(el, level);
            }
            i++;
        }
    }
    Es = Es + "\n" + addIndT + "</" + name + ">";

    return Es;
}

From source file:Main.java

public static void removeTextNodes(Node node) {
    NodeList nl = node.getChildNodes();

    int i = 0;/*from  w w  w  .j a  v a 2  s  . c o  m*/
    while (i < nl.getLength())
        if (nl.item(i).getNodeType() == Node.TEXT_NODE)
            node.removeChild(nl.item(i));
        else
            i++;
}

From source file:Main.java

static public String getElementText(Element elm) {
    Node node = elm.getFirstChild();
    if (node != null && node.getNodeType() == Node.TEXT_NODE)
        return node.getNodeValue();

    return null;// w w w  . j  ava 2 s  . c  o m
}