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

public static String getContentText(Element elementNode) {
    StringBuffer result = new StringBuffer();
    NodeList children = elementNode.getChildNodes();
    for (int c = 0; c < children.getLength(); c++) {
        Node child = children.item(c);
        if ((child.getNodeType() == Node.TEXT_NODE) && (child instanceof Text)) {
            String name = ((Text) child).getData();
            result.append(name);/*from  w  ww  .j  a  va 2 s .  c o  m*/
        }
    }
    return result.toString();
}

From source file:Main.java

@SuppressWarnings("fallthrough")
private static void getSetRec(final Node rootNode, final Set<Node> result, final Node exclude,
        final boolean com) {
    if (rootNode == exclude) {
        return;//from ww w  .  jav a 2s. c  o  m
    }
    switch (rootNode.getNodeType()) {
    case Node.ELEMENT_NODE:
        result.add(rootNode);
        Element el = (Element) rootNode;
        if (el.hasAttributes()) {
            NamedNodeMap nl = el.getAttributes();
            for (int i = 0; i < nl.getLength(); i++) {
                result.add(nl.item(i));
            }
        }
        //no return keep working
    case Node.DOCUMENT_NODE:
        for (Node r = rootNode.getFirstChild(); r != null; r = r.getNextSibling()) {
            if (r.getNodeType() == Node.TEXT_NODE) {
                result.add(r);
                while (r != null && r.getNodeType() == Node.TEXT_NODE) {
                    r = r.getNextSibling();
                }
                if (r == null) {
                    return;
                }
            }
            getSetRec(r, result, exclude, com);
        }
        return;
    case Node.COMMENT_NODE:
        if (com) {
            result.add(rootNode);
        }
        return;
    case Node.DOCUMENT_TYPE_NODE:
        return;
    default:
        result.add(rootNode);
    }
}

From source file:Main.java

public static String getText(Element node) {
    StringBuffer sb = new StringBuffer();
    NodeList list = node.getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {
        Node child = list.item(i);

        switch (child.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
        case Node.TEXT_NODE:
            sb.append(child.getNodeValue());
        }/*  w w w .j av  a 2  s  .  c  o m*/
    }

    return sb.toString();
}

From source file:Main.java

/**
 * Get text data of first XML {@code org.w3c.dom.Element} of given name.
 *
 * @param elem the parent XML Element//from   w ww  . ja va 2s.c  o m
 * @param name the name of the child text Element
 * @param emptyValue value to return if element exists, but is empty
 * @return text data of named child Element
 */
private static String getElementByTagName(Element elem, String name, String emptyValue) {
    NodeList nodeList = elem.getElementsByTagName(name);
    if (nodeList.getLength() == 0) {
        return null;
    }

    NodeList children = nodeList.item(0).getChildNodes();
    if (children.getLength() == 0 || children.item(0).getNodeType() != Node.TEXT_NODE) {
        return emptyValue;
    }
    return children.item(0).getNodeValue();
}

From source file:Main.java

public static int outputNode(Node outputNode, PrintWriter outputWriter, int curPos) {
    NodeList nodes = outputNode.getChildNodes();
    int curNodeNum;
    if (outputNode.getNodeType() == Node.TEXT_NODE) {
        outputWriter.print(outputNode.getNodeValue());
    } else {/*from   w w  w .  ja v a 2 s  .c  o m*/
        if (outputNode.getNodeName().equals("p")) {
            outputWriter.println();
        }

        for (curNodeNum = 0; curNodeNum < nodes.getLength(); curNodeNum++) {
            Node curNode = nodes.item(curNodeNum);
            curPos = outputNode(curNode, outputWriter, curPos);
            //System.out.println(curNode.getNodeName());
            //System.out.println(curNode.getNodeValue());
        }

    }
    return (curPos);
}

From source file:DomUtil.java

/**
 * Get the trimed text content of a node or null if there is no text
 *//*from w ww .j  a  v  a2s.c om*/
public static String getContent(Node n) {
    if (n == null)
        return null;
    Node n1 = DomUtil.getChild(n, Node.TEXT_NODE);

    if (n1 == null)
        return null;

    String s1 = n1.getNodeValue();
    return s1.trim();
}

From source file:Main.java

/**
 * Method getFullTextChildrenFromElement
 *
 * @param element//  ww  w  . j ava2 s . c om
 * @return the string of children
 */
public static String getFullTextChildrenFromElement(Element element) {
    StringBuilder sb = new StringBuilder();

    Node child = element.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.TEXT_NODE) {
            sb.append(((Text) child).getData());
        }
        child = child.getNextSibling();
    }

    return sb.toString();
}

From source file:Main.java

/**
 * A method to get the value of desire node from xml document
 * @param Node parent, xml's node object to get
 * @return String , value from provided node
 *///from   w w  w. j  a va  2 s. c o m
static public String getNodeValue(Node parent) {
    String ret = "";

    Node n = parent.getFirstChild();
    while (n != null) {
        if (n.getNodeType() == Node.TEXT_NODE) {
            try {
                ret = n.getNodeValue().trim();
            } catch (NullPointerException ex) {
                ret = "";
                break;
            }
        }
        n = n.getNextSibling();
    }
    return (ret);
}

From source file:Main.java

/**
 * This method sets value to given node//from  w  w w.  ja va2  s. co  m
 * 
 * @param inputNode
 *            Node to which value needs to be set
 * @param nodeValue
 *            Value to set
 * @throws IllegalArgumentException
 *             if input is invalid
 */
public static void setNodeTextValue(final Node inputNode, final String nodeValue)
        throws IllegalArgumentException {
    // Child list
    NodeList childList = null;

    // Validate input stream
    if (inputNode == null) {
        throw new IllegalArgumentException("Input Node cannot be null in XmlUtils.setNodeValue");
    }

    // Get child list
    childList = inputNode.getChildNodes();

    // If child nodes found
    if ((childList != null) && (childList.getLength() > 0)) {
        // Get child count
        final int childCount = childList.getLength();

        // For each child
        for (int childIndex = 0; childIndex < childCount; childIndex++) {
            final Node childNode = childList.item(childIndex);
            // Check if text node
            if ((childNode != null) && (childNode.getNodeType() == Node.TEXT_NODE)) {
                // Set value to text node
                childNode.setNodeValue(nodeValue);
                break;
            }
        }
    } else {
        // Create text node and set node value
        inputNode.appendChild(inputNode.getOwnerDocument().createTextNode(nodeValue));
    }
}

From source file:Main.java

public static List<Node> getTextAndElementChildren(Node node) {
    List<Node> result = new LinkedList<Node>();
    NodeList children = node.getChildNodes();
    if (children == null) {
        return result;
    }/* w  ww.ja  v  a  2  s  . co m*/
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (Node.ELEMENT_NODE == child.getNodeType() || Node.TEXT_NODE == child.getNodeType()) {
            result.add(child);
        }
    }
    return result;
}