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

/**
 * Sets the text value of an Element. if current text of the element is
 * replaced with the new text if text is null any
 * current text value is deleted.//from www .j a  v  a  2  s . co m
 * 
 * @param elem the Element for which the text value should be set
 * @param text the new text value of the element
 * @return true if the text could be set or false otherwise
 */
static public boolean setElementText(Node elem, Object text) {
    if (elem == null)
        return false; // Fehler
    // Find Text
    Node node = elem.getFirstChild();
    while (node != null) { // Find all Text nodes
        if (node.getNodeType() == Node.TEXT_NODE)
            break; // gefunden
        node = node.getNextSibling();
    }
    if (node != null) { // Set or remove text
        if (text != null)
            node.setNodeValue(text.toString());
        else
            elem.removeChild(node);
    } else if (text != null) { // Add Text
        elem.appendChild(elem.getOwnerDocument().createTextNode(text.toString()));
    }
    return true;
}

From source file:Main.java

/**
 * Returns the concatenated child text of the specified node.
 * This method only looks at the immediate children of type
 * Node.TEXT_NODE or the children of any child
 * node that is of type Node.CDATA_SECTION_NODE
 * for the concatenation.//w  w  w  .j av a2 s . co m
 *
 * @param node The node to look at.
 */
public static String getChildText(Node node) {

    // is there anything to do?
    if (node == null) {
        return null;
    }

    // concatenate children text
    StringBuffer str = new StringBuffer();
    Node child = node.getFirstChild();
    while (child != null) {
        short type = child.getNodeType();
        if (type == Node.TEXT_NODE) {
            str.append(child.getNodeValue());
        } else if (type == Node.CDATA_SECTION_NODE) {
            str.append(getChildText(child));
        }
        child = child.getNextSibling();
    }

    // return text value
    return str.toString();

}

From source file:Main.java

/** 
 * Decide if the node is text./*from   w ww.  ja va2 s . c  o  m*/
 * 
 * @param n Node to check
 * @return True if node is text; otherwise retur false
 */
public static boolean isTextNode(Node n) {
    //Sanity check
    if (n == null)
        return false;

    //Get node type
    short nodeType = n.getNodeType();

    //Check type and return result
    return nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.TEXT_NODE;
}

From source file:Main.java

public static String domNode2String(Node node, boolean escapeStrings) {
    String ret = "";
    switch (node.getNodeType()) {

    case Node.DOCUMENT_NODE:
        // recurse on each child
        NodeList nodes = node.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                ret += domNode2String(nodes.item(i), escapeStrings);
            }//from   w w  w .  j av a2  s.c  o  m
        }
        break;

    case Node.ELEMENT_NODE:
        String name = node.getNodeName();
        ret += "<" + name;
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node current = attributes.item(i);
            ret += " " + current.getNodeName() + "=\""
                    + ((escapeStrings) ? escapeStringForXML(current.getNodeValue()) : current.getNodeValue())
                    + "\"";
        }
        ret += ">";

        // recurse on each child
        NodeList children = node.getChildNodes();
        if (children != null) {
            for (int i = 0; i < children.getLength(); i++) {
                ret += domNode2String(children.item(i), escapeStrings);
            }
        }

        ret += "</" + name + ">";
        break;

    case Node.TEXT_NODE:
        ret += (escapeStrings) ? escapeStringForXML(node.getNodeValue()) : node.getNodeValue();
        break;
    case Node.COMMENT_NODE:
        ret += "<!--" + node.getNodeValue() + "-->";
        break;
    }
    return ret;
}

From source file:Main.java

public static void transformToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException,
        IOException {//ww w. j  ava 2  s.  c o  m

    if (node.getNodeType() == Node.TEXT_NODE) {
        byte[] bytes = tranformTextNodeToByteArray(node);
        os.write(bytes);
    } else {
        transformNonTextNodeToOutputStream(node, os, omitXmlDeclaration);
    }
}

From source file:Main.java

/**
 * _more_/*from w  w w.j  a  v  a2  s  . co m*/
 *
 * @param html _more_
 * @param node _more_
 */
public static void toHtml(StringBuffer html, Node node) {
    switch (node.getNodeType()) {

    case Node.ELEMENT_NODE: {
        NodeList children = node.getChildNodes();
        int numChildren = children.getLength();
        html.append("<b>" + node.getNodeName().replace("_", " ") + "</b>");
        html.append(": ");

        for (int i = 0; i < numChildren; i++) {
            Node child = children.item(i);
            if (((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))) {
                String v = child.getNodeValue();
                if (v == null) {
                    continue;
                }
                if (v.trim().length() == 0) {
                    continue;
                }
                html.append(v);
                html.append(" ");
            }
        }
        boolean didone = false;
        NamedNodeMap nnm = node.getAttributes();
        if (nnm != null) {
            for (int i = 0; i < nnm.getLength(); i++) {
                Attr attr = (Attr) nnm.item(i);
                String attrName = attr.getNodeName();
                if (attrName.startsWith("xmlns") || attrName.startsWith("xsi:")) {
                    continue;
                }
                if (!didone) {
                    html.append("<ul>");
                    didone = true;
                }
                html.append(attrName.replace("_", " ") + "=" + attr.getNodeValue());
                html.append("<br>\n");
            }
        }
        int cnt = 0;
        for (int i = 0; i < numChildren; i++) {
            Node child = children.item(i);
            if (((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))) {
                continue;
            }
            if (!didone) {
                html.append("<ul>");
                didone = true;
            }
            if (cnt > 0) {
                html.append("<br>");
            }
            toHtml(html, child);
            cnt++;
        }
        if (didone) {
            html.append("</ul>");
        }
        break;
    }
    }
}

From source file:Main.java

public static String getElementText(Element e) {
    final StringBuilder val = new StringBuilder();
    for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) {
        if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE) {
            val.append(n.getNodeValue());
        }//ww  w  .  ja va  2  s. com
    }
    return val.toString();
}

From source file:Main.java

public static void walkNodes(Node nodeIn, StringBuffer sb, String sPad) {
    if (nodeIn == null)
        return;//w w  w  . j a v  a2 s  . c om
    NamedNodeMap map = nodeIn.getAttributes();
    if (map != null)
        for (int i = 0; i < map.getLength(); i++) {
            Node n = map.item(i);
            if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
                sb.append(sPad + "   Attribute:" + n.getNodeName() + " = " + n.getNodeValue() + '\n');
            }
        }
    NodeList nodes = nodeIn.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            sb.append(sPad + "Element: " + n.getNodeName() + '\n');
        }
        if (n.getNodeType() == Node.TEXT_NODE) {
            sb.append(sPad + "   Value: = " + n.getNodeValue() + '\n');
        }
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            walkNodes(n, sb, sPad + "   ");
        }
    }
}

From source file:Main.java

public static String getTextFromNode(Node node, String defaultValue) {
    if (node == null) {
        return defaultValue;
    }//  w w w .j  av  a 2  s.  com
    if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        return ((Attr) node).getValue();
    } else if (node.getNodeType() == Node.TEXT_NODE) {
        return node.getNodeValue();
    } else {
        StringBuffer text = new StringBuffer();
        getTextFromNode(node, text, true);
        return text.toString().trim();
    }
}

From source file:Main.java

public static Object transformXmlNodesIntoMap(Node node) {
    Map<String, Object> nodeMap = new HashMap<String, Object>();

    NodeList subNodes = node.getChildNodes();

    NamedNodeMap nodeAttrs = node.getAttributes();
    for (int nodeAttrIdx = 0; nodeAttrIdx < nodeAttrs.getLength(); nodeAttrIdx++) {
        Node attrNode = nodeAttrs.item(nodeAttrIdx);
        // nodeMap.put("@"+attrNode.getNodeName(),
        // attrNode.getTextContent());
        nodeMap.put("@" + attrNode.getNodeName(), attrNode.getNodeValue());
    }//from w w  w.  j a va 2s .  co  m

    if (nodeAttrs.getLength() == 0)
        if (subNodes.getLength() == 0)
            return "";
        else if (subNodes.getLength() == 1 && subNodes.item(0).getNodeType() == Node.TEXT_NODE)
            // return subNodes.item(0).getTextContent();
            return subNodes.item(0).getNodeValue();

    for (int subNodeIdx = 0; subNodeIdx < subNodes.getLength(); subNodeIdx++) {
        Node subNode = subNodes.item(subNodeIdx);

        if (subNode.getNodeType() == Node.TEXT_NODE) {
            // nodeMap.put(subNode.getNodeName(), subNode.getTextContent());
            nodeMap.put(subNode.getNodeName(), subNode.getNodeValue());
        } else {
            if (nodeMap.containsKey(subNode.getNodeName())) {
                Object subObject = nodeMap.get(subNode.getNodeName());
                if (subObject instanceof List<?>) {
                    ((List<Object>) subObject).add(transformXmlNodesIntoMap(subNode));
                } else {
                    List<Object> subObjectList = new ArrayList<Object>();
                    subObjectList.add(subObject);
                    subObjectList.add(transformXmlNodesIntoMap(subNode));
                    nodeMap.put(subNode.getNodeName(), subObjectList);
                }
            } else {
                nodeMap.put(subNode.getNodeName(), transformXmlNodesIntoMap(subNode));
            }
        }

    }
    return nodeMap;
}