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

/**
 * Format generated xml doc by indentation
 *
 * @param node   For java, rather than GWT
 * @param indent//  ww w .j  av a  2 s.c  o  m
 * @return
 */
public static String format(Node node, String indent) {
    StringBuilder formatted = new StringBuilder();

    if (node.getNodeType() == Node.ELEMENT_NODE) {
        StringBuilder attributes = new StringBuilder();
        for (int k = 0; k < node.getAttributes().getLength(); k++) {
            attributes.append(" ");
            attributes.append(node.getAttributes().item(k).getNodeName());
            attributes.append("=\"");
            attributes.append(node.getAttributes().item(k).getNodeValue());
            attributes.append("\"");
        }

        formatted.append(indent);
        formatted.append("<");
        formatted.append(node.getNodeName());
        formatted.append(attributes.toString());
        if (!node.hasChildNodes()) {
            formatted.append("/>\n");
            return formatted.toString();
        }
        if ((node.hasChildNodes() && node.getFirstChild().getNodeType() == Node.TEXT_NODE)) {
            formatted.append(">");
        } else {
            formatted.append(">\n");
        }

        for (int i = 0; i < node.getChildNodes().getLength(); i++) {
            formatted.append(format(node.getChildNodes().item(i), indent + "   "));
        }

        if (node.hasChildNodes() && node.getFirstChild().getNodeType() != Node.TEXT_NODE) {
            formatted.append(indent);
        }
        formatted.append("</");
        formatted.append(node.getNodeName());
        formatted.append(">\n");
    } else {
        String value = node.getTextContent().trim();
        if (value.length() > 0) {
            formatted.append(value);
        }
    }
    return formatted.toString();
}

From source file:Main.java

public static String format(Node n, int indentLevel) {
    String indent = "";
    for (int i = 0; i < indentLevel; i++) {
        indent += "\t";
    }/*from  www  .  j av a 2  s .c  om*/

    StringBuilder result = new StringBuilder();
    result.append(indent);
    if (n.getNodeType() == Node.ELEMENT_NODE) {
        result.append("<");
        result.append(n.getNodeName());
        NamedNodeMap attrs = n.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            result.append(" ");
            result.append(attr.getNodeName());
            result.append("=\"");
            result.append(attr.getNodeValue());
            result.append("\"");
        }
        result.append(">\n");
    }

    if (n.getNodeType() == Node.TEXT_NODE) {
        String str = n.getNodeValue();
        result.append(str + "\n");
    }

    NodeList childNodes = n.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node n2 = childNodes.item(i);
        if (isEmptyTextNode(n2)) {
            continue;
        }
        result.append(format(n2, indentLevel + 1));
    }

    if (n.getNodeType() == Node.ELEMENT_NODE) {
        result.append(indent);
        result.append("</");
        result.append(n.getNodeName());
        result.append(">\n");
    }
    return result.toString();
}

From source file:Main.java

/**
 * Remove any whitespace text nodes from the DOM. Calling this before saving
 * a formatted document will fix the formatting indentation of elements
 * loaded from a different document.//w  ww  . j a  va2  s. c o m
 * 
 * @param node
 *            Node to remove whitespace nodes from
 * @param deep
 *            Should this method recurse into the node's children?
 */
public static void removeWhitespace(Node node, boolean deep) {
    NodeList children = node.getChildNodes();
    int length = children.getLength();
    for (int i = 0; i < length; i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE && length > 1) {
            Node previous = child.getPreviousSibling();
            Node next = child.getNextSibling();
            if ((previous == null || previous.getNodeType() == Node.ELEMENT_NODE
                    || previous.getNodeType() == Node.COMMENT_NODE)
                    && (next == null || next.getNodeType() == Node.ELEMENT_NODE
                            || next.getNodeType() == Node.COMMENT_NODE)) {
                String content = child.getTextContent();
                if (content.matches("\\s*")) //$NON-NLS-1$
                {
                    node.removeChild(child);
                    i--;
                    length--;
                }
            }
        } else if (deep && child.getNodeType() == Node.ELEMENT_NODE) {
            removeWhitespace(child, deep);
        }
    }
}

From source file:Main.java

/**
 * Helper method to get a list of only {@link Text} and {@link Element} typed {@link Node}s.
 * This is partially to workaround the difficulty of working with the {@link NodeList} object.
 * /*from  ww  w .j a v  a 2 s . c  o  m*/
 * @param node
 *            the node whose children to get
 * 
 * @return the filtered list of child nodes
 */
private static List<Node> getChildNodes(Node node) {
    List<Node> children = new ArrayList<Node>();
    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node child = nl.item(i);
        short type = child.getNodeType();
        if (type == Node.ELEMENT_NODE) {
            children.add(child);
        } else if (type == Node.TEXT_NODE) {
            String text = ((Text) child).getTextContent().trim();
            if (text.length() > 0) {
                children.add(child);
            }
        }
    }
    return children;
}

From source file:Main.java

/**
 * This method will return the content of this particular <code>element</code>.
 * For example,//from w  ww  . jav  a2  s .c  om
 * <p/>
 * <pre>
 *    <result>something_1</result>
 * </pre>
 * When the {@link org.w3c.dom.Element} <code>&lt;result&gt;</code> is passed in as
 * argument (<code>element</code> to this method, it returns the content of it,
 * namely, <code>something_1</code> in the example above.
 *
 * @return
 */
public static String getContent(Element element) {
    StringBuilder paramValue = new StringBuilder();
    NodeList childNodes = element.getChildNodes();
    for (int j = 0; j < childNodes.getLength(); j++) {
        Node currentNode = childNodes.item(j);
        if (currentNode != null && currentNode.getNodeType() == Node.TEXT_NODE) {
            String val = currentNode.getNodeValue();
            if (val != null) {
                paramValue.append(val.trim());
            }
        }
    }
    return paramValue.toString().trim();
}

From source file:Main.java

/**
 * /* w w w . j av  a 2  s  .  co m*/
 * @param currentNode
 * @param tagName
 * @param attributeValue
 * @return
 */
public static String getTextContentByElementNameANDAttributeValue(Node currentNode, String tagName,
        String attributeValue) {
    String result = "";

    NodeList childNodeList = currentNode.getChildNodes();
    for (int i = 0; i < childNodeList.getLength(); i++) {
        Node childNode = childNodeList.item(i);

        switch (childNode.getNodeType()) {
        case Node.DOCUMENT_NODE:
            break;
        case Node.ELEMENT_NODE:
            Element childElement = (Element) childNodeList.item(i);
            // logger.debug("childElement name : " + childElement.getTagName());
            if (childElement != null && childElement.getNodeName().equals(tagName)) {
                NamedNodeMap attributes = childElement.getAttributes();
                for (int j = 0; j < attributes.getLength(); j++) {
                    Node current = attributes.item(j);

                    if (current.getNodeName().equals("type") && current.getNodeValue().equals(attributeValue)) {
                        result = childElement.getTextContent();
                        break;
                    }
                }
            }
        case Node.TEXT_NODE:
            // logger.debug("textElement name : " + currentNode.getNodeValue());
            break;
        case Node.COMMENT_NODE:
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            break;
        case Node.ENTITY_REFERENCE_NODE:
            break;
        case Node.DOCUMENT_TYPE_NODE:
            break;
        }
    }

    return result;
}

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());
    }/* w w w  .jav  a  2 s.  c  om*/

    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();

    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());
        } 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;
}

From source file:Main.java

public static void setByPath(Node doc, String path, String value) {
    Node node = getNodeByPath(doc, path);
    if (node.hasChildNodes() && node.getFirstChild().getNodeType() == Node.TEXT_NODE) {
        node.getFirstChild().setTextContent(value);
    } else {//from w w  w  . j  a v  a  2  s . com
        node.setNodeValue(value);
    }
}

From source file:Main.java

/**
 * * Convenience method to transfer a node (and all of its children) from one
 * * DOM XML document to another./*from   w  ww  . ja  v a2s. co  m*/
 * *
 * * Note: this method is recursive.
 * *
 * * @param current the current Element to append the transfer to
 * * @param target the target document for the transfer
 * * @param n the Node to transfer
 * * @return Element the current element.
 */
public static Element transferNode(Element current, Document target, Node n) {
    String name = n.getNodeName();
    String value = n.getNodeValue();
    short type = n.getNodeType();

    if (type == Node.ELEMENT_NODE) {
        // create a new element for this node in the target document
        Element e = target.createElement(name);

        // move all the attributes over to the target document
        NamedNodeMap attrs = n.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node a = attrs.item(i);
            e.setAttribute(a.getNodeName(), a.getNodeValue());
        }

        // get the children for this node
        NodeList children = n.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            // transfer each of the children to the new document
            transferNode(e, target, children.item(i));
        }

        // append the node to the target document element
        current.appendChild(e);
    } else if (type == Node.TEXT_NODE) {
        Text text = target.createTextNode(value);
        current.appendChild(text);
    }

    return current;
}

From source file:Main.java

public static String textContent(Node node) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        StringBuffer sb = new StringBuffer();
        Node nextChild = node.getFirstChild();
        while (nextChild != null) {
            sb.append(textContent(nextChild));
            nextChild = nextChild.getNextSibling();
        }//from  w w w.ja  v a2  s. c om
        return sb.toString();
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        return node.getNodeValue();
    default:
        return "";
    }
}