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

/**
 * Generates an XPath expression that will return only the given node as its
 * result. This method only works for element, text, document and PI nodes.
 *
 * @param node the node to generate an XPath expression for. This node must
 * be an element node, a text node, a document node, or a processing
 * instruction node.// w  w w. ja v a  2 s.c om
 * @return an XPath expression that will return only the given node as its
 * result.
 * @exception IllegalArgumentException if the given node is not an element,
 * text, document or PI node.
 */
public static String getXPathExprFromNode(Node node) throws IllegalArgumentException {
    short nodeType = getNodeType(node);

    switch (nodeType) {
    case Node.ELEMENT_NODE:
    case Node.TEXT_NODE:
    case Node.PROCESSING_INSTRUCTION_NODE:
        return getXPathFromVector(getVectorPathFromNode(node));

    case Node.DOCUMENT_NODE:
        return "/";

    default:
        throw new IllegalArgumentException("Only works for element, text, " + "document, and PI nodes.");
    }
}

From source file:Main.java

private static void convert(Node toCopy, Node saveTo, Document doc) {
    Node newNode;/*  w w w.ja v a2  s.co  m*/
    switch (toCopy.getNodeType()) {
    case Node.ELEMENT_NODE:
        Element newElement = doc.createElementNS(toCopy.getNamespaceURI(), toCopy.getNodeName());
        newNode = newElement;
        Element baseElement = (Element) toCopy;
        NamedNodeMap children = baseElement.getAttributes();
        for (int i = 0; i < children.getLength(); i++) {
            convertAttribute((Attr) children.item(i), newElement, doc);
        }
        break;
    case Node.TEXT_NODE:
        newNode = doc.createTextNode(toCopy.getTextContent());
        break;
    default:
        newNode = null;
    }
    if (newNode != null) {
        NodeList children = toCopy.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            convert(children.item(i), newNode, doc);
        }

        saveTo.appendChild(newNode);
    }
}

From source file:Main.java

private static void getSetRec(final Node rootNode, final Set<Node> result, final Node exclude,
        final boolean com) {
    if (rootNode == exclude) {
        return;/*from www  .  j  av a 2  s.com*/
    }
    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

/**
 * Sets the Node n's text value to that of s.
 * @param Node - the node whose text will be set.
 * If that node isn't a text node. It will set the first childs that is a text node.
 * The rest will be ignored//from  w  w w  .ja v  a 2s  .  c o m
 * @param String - s the text that will be set to the node.
 */
public static void setNodeText(Node n, String s) {
    if (n == null || s == null || s.length() == 0) {
        System.err.println("An argument is null");
        return;
    }

    if (n.getNodeType() == Node.TEXT_NODE) {
        n.setNodeValue(s);
        return;
    }

    NodeList nl = n.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node cn = nl.item(i);
        if (cn == null)
            continue;
        if (cn.getNodeType() == Node.TEXT_NODE) {
            cn.setNodeValue(s);
            break;
        }
    }
}

From source file:Main.java

private static String getTextContent(final 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://from  w  w  w  .ja  va  2  s . c  o  m
        return null;
    }
}

From source file:Main.java

/**
 * Clones the given DOM node into the given DOM document.
 * //from   w w  w  . ja  v  a  2  s . c  o m
 * @param node
 *            The DOM node to clone.
 * @param doc
 *            The target DOM document.
 * @return The cloned node in the target DOM document.
 */
public static Node cloneNode(Node node, Document doc) {
    Node clone = null;
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        clone = doc.createElement(node.getNodeName());
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attrNode = attrs.item(i);
            Attr attrClone = doc.createAttribute(attrNode.getNodeName());
            attrClone.setNodeValue(attrNode.getNodeValue());
            ((Element) clone).setAttributeNode(attrClone);
        }

        // Iterate through each child nodes.
        NodeList childNodes = node.getChildNodes();
        if (childNodes != null) {
            for (int i = 0; i < childNodes.getLength(); i++) {
                Node childNode = childNodes.item(i);
                if (childNode == null) {
                    continue;
                }
                Node childClone = cloneNode(childNode, doc);
                if (childClone == null) {
                    continue;
                }
                clone.appendChild(childClone);
            }
        }
        break;

    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        clone = doc.createTextNode(node.getNodeName());
        clone.setNodeValue(node.getNodeValue());
        break;
    }
    return clone;
}

From source file:DOMParsarDemo.java

public void printElement(Node node) {
    if (node.getNodeType() != Node.TEXT_NODE) {
        Node child = node.getFirstChild();
        while (child != null) {
            if (node.getNodeName().equals("distance")) {
                if (child.getNodeName().equals("value")) {
                    System.out.println(child.getFirstChild().getNodeValue());
                }//from www. ja v a  2  s. co m
            }
            printElement(child);
            child = child.getNextSibling();
        }
    }
}

From source file:Main.java

static public void setElementText(Element elm, String text) {
    Node node = elm.getFirstChild();
    if (node == null) {
        if (text != null)
            elm.appendChild(elm.getOwnerDocument().createTextNode(text));
    } else if (node.getNodeType() == Node.TEXT_NODE) {
        if (text == null)
            node.getParentNode().removeChild(node);
        else// www  . ja v a  2 s . c om
            node.setNodeValue(text);
    } else if (text != null) {
        Text textNode = node.getOwnerDocument().createTextNode(text);
        elm.insertBefore(textNode, elm.getFirstChild());
    }
}

From source file:Main.java

private static void removeEmptyTextNodes(Node parentNode) {
    Node childNode = parentNode.getFirstChild();
    while (childNode != null) {
        // grab the "nextSibling" before the child node is removed
        Node nextChild = childNode.getNextSibling();

        short nodeType = childNode.getNodeType();
        if (nodeType == Node.TEXT_NODE) {
            boolean containsOnlyWhitespace = childNode.getNodeValue().trim().isEmpty();
            if (containsOnlyWhitespace) {
                parentNode.removeChild(childNode);
            }/*from   www  .j a  v  a 2  s  .com*/
        }
        childNode = nextChild;
    }
}

From source file:Main.java

static void printElement(Element element, String indent) {
    System.out.println("Element '" + element.getNodeName() + "'");
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        switch (child.getNodeType()) {
        case Node.ELEMENT_NODE:
            printElement((Element) child, indent + "\t");
            break;
        case Node.ATTRIBUTE_NODE:
            Attr attr = (Attr) child;
            System.out.println("\tAttribute: '" + attr.getName() + "' = '" + attr.getValue() + "'");
            break;
        case Node.COMMENT_NODE:
            Comment comment = (Comment) child;
            System.out.println("\tComment: '" + comment.getData() + "'");
            break;
        case Node.CDATA_SECTION_NODE:
            CharacterData cdata = (CharacterData) child;
            System.out.println("\tCDatat: '" + cdata.getData() + "'");
            break;
        case Node.TEXT_NODE:
            Text text = (Text) child;
            System.out.println("\tText: '" + text.getData() + "'");
            break;
        default:// ww  w. j a v  a2s .  co  m
            System.out.println("\tUnknown node type: '" + child.getNodeType() + "'");
            break;
        }
    }
}