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:MainClass.java

public void processNode(Node node, String spacer) throws IOException {
    if (node == null)
        return;// w w w  .j  av a  2 s .  c  om
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        String name = node.getNodeName();
        System.out.print(spacer + "<" + name);
        NamedNodeMap nnm = node.getAttributes();
        for (int i = 0; i < nnm.getLength(); i++) {
            Node current = nnm.item(i);
            System.out.print(" " + current.getNodeName() + "= " + current.getNodeValue());
        }
        System.out.print(">");
        NodeList nl = node.getChildNodes();
        if (nl != null) {
            for (int i = 0; i < nl.getLength(); i++) {
                processNode(nl.item(i), "");
            }
        }
        System.out.println(spacer + "</" + name + ">");
        break;
    case Node.TEXT_NODE:
        System.out.print(node.getNodeValue());
        break;
    case Node.CDATA_SECTION_NODE:
        System.out.print("" + node.getNodeValue() + "");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.print("&" + node.getNodeName() + ";");
        break;
    case Node.ENTITY_NODE:
        System.out.print("<ENTITY: " + node.getNodeName() + "> </" + node.getNodeName() + "/>");
        break;
    case Node.DOCUMENT_NODE:
        NodeList nodes = node.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                processNode(nodes.item(i), "");
            }
        }
        break;
    case Node.DOCUMENT_TYPE_NODE:
        DocumentType docType = (DocumentType) node;
        System.out.print("<!DOCTYPE " + docType.getName());
        if (docType.getPublicId() != null) {
            System.out.print(" PUBLIC " + docType.getPublicId() + " ");
        } else {
            System.out.print(" SYSTEM ");
        }
        System.out.println(" " + docType.getSystemId() + ">");
        break;
    default:
        break;
    }
}

From source file:Main.java

private static boolean serializeXmlNode(Node node, Writer writer, boolean includeNode) throws IOException {
    if (node == null) {
        return false;
    }//from   ww w . j a  va  2  s  . c o m
    short type = node.getNodeType();
    boolean result = true;
    switch (type) {
    case Node.ATTRIBUTE_NODE: {
        String text = ((Attr) node).getValue();
        writer.write(text);
        break;
    }
    case Node.TEXT_NODE: {
        String text = ((Text) node).getData();
        writer.write(text);
        break;
    }
    case Node.ELEMENT_NODE: {
        Element element = (Element) node;
        if (includeNode) {
            serializeXML(element, writer, false);
        } else {
            Node child = element.getFirstChild();
            while (child != null) {
                serializeXmlNode(child, writer, true);
                child = child.getNextSibling();
            }
        }
        break;
    }
    case Node.DOCUMENT_NODE: {
        Document doc = (Document) node;
        serializeXmlNode(doc.getDocumentElement(), writer, includeNode);
        break;
    }
    default:
        result = false;
        break;
    }
    return result;
}

From source file:Main.java

/**
 * Method to get the value of "Value" node
 * If <class>unescape<class> is set to false, xml escaped chars will not
 * be unescaped.//  w  w w  .j  a  va 2s . c o  m
 */
public static String getValueOfValueNodeNoTrim(Node n, boolean unescape) {
    NodeList textNodes = n.getChildNodes();
    Node textNode;
    StringBuffer value = new StringBuffer("");
    for (int j = 0; j < textNodes.getLength(); j++) {
        textNode = textNodes.item(j);
        String text = null;
        if (textNode.getNodeType() == Node.TEXT_NODE) {
            text = textNode.getNodeValue();
        } else if (textNode.getNodeType() == Node.ELEMENT_NODE) {
            text = print(textNode);
        }
        if (text != null && unescape) {
            value.append(unescapeSpecialCharacters(text));
        } else {
            value.append(text);
        }
    }
    return value.toString();
}

From source file:Main.java

/**
 * Check if the element contains just a text/cdata, in which case return
 * that value. Else return null;/*from ww  w . jav a2 s  .c o m*/
 *
 * @param element
 * @return null if this is not a textElement as we see it. Value of single
 *         text/CData child otherwise
 */
private static String getElementValue(Element element) {
    if (element.getAttributes().getLength() > 0) {
        return null;
    }

    NodeList children = element.getChildNodes();
    String value = null;
    int nbrChildren = children.getLength();
    for (int i = 0; i < nbrChildren; i++) {
        Node child = children.item(i);
        short childType = child.getNodeType();
        if (childType == Node.ELEMENT_NODE) {
            return null;
        }
        if (childType == Node.CDATA_SECTION_NODE || childType == Node.TEXT_NODE) {
            if (value != null) {
                return null;
            }
            value = child.getNodeValue();
        }
    }
    return value;
}

From source file:Main.java

public static String text(Node node) {
    StringBuffer sb = new StringBuffer();
    Node n = node.getFirstChild();
    while (n != null) {
        if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE)
            sb.append(n.getNodeValue());
        n = n.getNextSibling();//from ww  w . j  a v a  2  s.c o m
    }
    return sb.toString();
}

From source file:Main.java

@SuppressWarnings("null")
public static void copyInto(Node src, Node dest) throws DOMException {

    Document factory = dest.getOwnerDocument();

    //Node start = src;
    Node parent = null;/*from   www  . ja  va 2  s  .c  o m*/
    Node place = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr) attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                /*
                 if (domimpl && !attr.getSpecified()) {
                 ((Attr) element.getAttributeNode(attrName)).setSpecified(false);
                 }
                 */
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException(
                    "can't copy node type, " + type + " (" + node.getNodeName() + ')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place = place.getFirstChild();
            dest = node;
        } else if (parent == null) {
            place = null;
        } else {
            // advance
            place = place.getNextSibling();
            while (place == null && parent != null && dest != null) {
                place = parent.getNextSibling();
                parent = parent.getParentNode();
                dest = dest.getParentNode();
            }
        }

    }

}

From source file:Main.java

/**
 * Clone given Node into target Document. If targe is null, same Document will be used.
 * If deep is specified, all children below will also be cloned.
 *//*  www  .j av  a 2 s  .  c  o  m*/
public final static Node cloneNode(Node node, Document target, boolean deep) throws DOMException {
    if ((target == null) || (node.getOwnerDocument() == target)) {
        // same Document
        return node.cloneNode(deep);
    } else {
        //DOM level 2 provides this in Document, so once xalan switches to that,
        //we can take out all the below and just call target.importNode(node, deep);
        //For now, we implement based on the javadocs for importNode
        Node newNode;
        int nodeType = node.getNodeType();

        switch (nodeType) {
        case Node.ATTRIBUTE_NODE:
            newNode = target.createAttribute(node.getNodeName());

            break;

        case Node.DOCUMENT_FRAGMENT_NODE:
            newNode = target.createDocumentFragment();

            break;

        case Node.ELEMENT_NODE:

            Element newElement = target.createElement(node.getNodeName());
            NamedNodeMap nodeAttr = node.getAttributes();

            if (nodeAttr != null) {
                for (int i = 0; i < nodeAttr.getLength(); i++) {
                    Attr attr = (Attr) nodeAttr.item(i);

                    if (attr.getSpecified()) {
                        Attr newAttr = (Attr) cloneNode(attr, target, true);
                        newElement.setAttributeNode(newAttr);
                    }
                }
            }

            newNode = newElement;

            break;

        case Node.ENTITY_REFERENCE_NODE:
            newNode = target.createEntityReference(node.getNodeName());

            break;

        case Node.PROCESSING_INSTRUCTION_NODE:
            newNode = target.createProcessingInstruction(node.getNodeName(), node.getNodeValue());

            break;

        case Node.TEXT_NODE:
            newNode = target.createTextNode(node.getNodeValue());

            break;

        case Node.CDATA_SECTION_NODE:
            newNode = target.createCDATASection(node.getNodeValue());

            break;

        case Node.COMMENT_NODE:
            newNode = target.createComment(node.getNodeValue());

            break;

        case Node.NOTATION_NODE:
        case Node.ENTITY_NODE:
        case Node.DOCUMENT_TYPE_NODE:
        case Node.DOCUMENT_NODE:
        default:
            throw new IllegalArgumentException("Importing of " + node + " not supported yet");
        }

        if (deep) {
            for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
                newNode.appendChild(cloneNode(child, target, true));
            }
        }

        return newNode;
    }
}

From source file:Main.java

/**
 * Return the content of the given element.
 * <p/>/*  w w  w. j  a v  a  2  s. c om*/
 * We will descend to an arbitrary depth looking for the first text node.
 * <p/>
 * Note that the parser may break what was originally a single string of
 * pcdata into multiple adjacent text nodes. Xerces appears to do this when
 * it encounters a '$' in the text, not sure if there is specified behavior,
 * or if its parser specific.
 * <p/>
 * Here, we will congeal adjacent text nodes.
 * <p/>
 * We will NOT ignore text nodes that have only whitespace.
 */
public static String getContent(Element e) {

    String content = null;

    if (e != null) {

        // find the first inner text node,
        Text t = findText(e, false);
        if (t != null) {
            // we have at least some text
            StringBuilder b = new StringBuilder();
            while (t != null) {
                b.append(t.getData());
                Node n = t.getNextSibling();

                t = null;
                if (n != null && ((n.getNodeType() == Node.TEXT_NODE)
                        || (n.getNodeType() == Node.CDATA_SECTION_NODE))) {
                    t = (Text) n;
                }
            }
            content = b.toString();
        }
    }

    return content;
}

From source file:Main.java

private static void getChildrenText(NodeList nodeList, StringBuffer buf) {
    int len = nodeList.getLength();
    for (int i = 0; i < len; ++i) {
        Node child = nodeList.item(i);
        while (child != null) {
            short nodeType = child.getNodeType();
            switch (nodeType) {
            case Node.TEXT_NODE:
                buf.append(child.getNodeValue());
                break;
            case Node.ELEMENT_NODE:
                getChildrenText(child.getChildNodes(), buf);
                break;
            }/* w ww . j  av  a 2s  . co m*/
            child = child.getNextSibling();
        }
    }
}

From source file:Main.java

/**
 * For compatibility reasons the following is required:
 * If the value of a text node is to be changed, but a CDATA section with this name
 * already exists, the CDATA section is removed an a text node is created or changed.
 *
 * If the value of a CDATA section is to be changed, but a text node with this name
 * already exists, the text node is removed an a CDATA section is created or changed.
 *
 *//* w w w . ja v  a 2s .co  m*/
public static void setElementText(Element e, String newValue, boolean cdata) {
    if (e == null) {
        return;
    }

    Node node = null;

    NodeList children = e.getChildNodes();

    if (children != null) {
        Node childToRemove = null;
        boolean changed = false;

        int listLength = children.getLength();

        for (int i = 0; i < listLength; i++) {
            node = children.item(i);

            int nodeType = node.getNodeType();

            if (nodeType == Node.TEXT_NODE) {
                if (cdata) {
                    childToRemove = node;
                } else {
                    node.setNodeValue(newValue);
                    changed = true;
                }
            }

            if (nodeType == Node.CDATA_SECTION_NODE) {
                if (!cdata) {
                    childToRemove = node;
                } else {
                    node.setNodeValue(newValue);
                    changed = true;
                }

            }
        }

        if (childToRemove != null) {
            // System.out.println("removing child " + childToRemove.getNodeValue());
            childToRemove.setNodeValue("");
            e.removeChild(childToRemove);
        }

        if (changed) {
            return;
        }
    }

    Document doc = e.getOwnerDocument();

    if (cdata) {
        node = doc.createCDATASection(newValue);
    } else {
        node = doc.createTextNode(newValue);
    }

    e.appendChild(node);
}