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

/** Prints the specified node, then prints all of its children. */

public static void printDOM(Node node) {

    int type = node.getNodeType();

    switch (type) {

    // print the document element
    case Node.DOCUMENT_NODE: {
        System.out.print("<?xml version=\"1.0\" ?>");
        printDOM(((Document) node).getDocumentElement());
        break;// w  w w.  ja v  a 2  s.  c o m
    }

    // print element with attributes
    case Node.ELEMENT_NODE: {
        System.out.println();
        System.out.print("<");
        System.out.print(node.getNodeName());
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            System.out.print(" " + attr.getNodeName().trim() + "=\"" + attr.getNodeValue().trim() + "\"");
        }
        System.out.print(">");
        NodeList children = node.getChildNodes();

        if (children != null) {
            int len = children.getLength();
            for (int i = 0; i < len; i++)
                printDOM(children.item(i));
        }
        break;
    }

    // handle entity reference nodes

    case Node.ENTITY_REFERENCE_NODE: {
        System.out.print("&");
        System.out.print(node.getNodeName().trim());
        System.out.print(";");
        break;
    }

    // print cdata sections
    case Node.CDATA_SECTION_NODE: {
        System.out.print("<![CDATA[");
        System.out.print(node.getNodeValue().trim());
        System.out.print("]]>");
        break;
    }

    // print text
    case Node.TEXT_NODE: {
        System.out.println();
        System.out.print(node.getNodeValue().trim());
        break;
    }

    // print processing instruction

    case Node.PROCESSING_INSTRUCTION_NODE: {
        System.out.print("<?");
        System.out.print(node.getNodeName().trim());
        String data = node.getNodeValue().trim();
        {
            System.out.print(" ");
            System.out.print(data);
        }
        System.out.print("?>");
        break;
    }
    }

    if (type == Node.ELEMENT_NODE) {
        System.out.println();
        System.out.print("</");
        System.out.print(node.getNodeName().trim());
        System.out.print('>');
    }
}

From source file:DOMDump.java

private static void dumpLoop(Node node, String indent) {
    switch (node.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
        System.out.println(indent + "CDATA_SECTION_NODE");
        break;//from  ww  w  .j ava 2s .co  m
    case Node.COMMENT_NODE:
        System.out.println(indent + "COMMENT_NODE");
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
        break;
    case Node.DOCUMENT_NODE:
        System.out.println(indent + "DOCUMENT_NODE");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        System.out.println(indent + "DOCUMENT_TYPE_NODE");
        break;
    case Node.ELEMENT_NODE:
        System.out.println(indent + "ELEMENT_NODE");
        break;
    case Node.ENTITY_NODE:
        System.out.println(indent + "ENTITY_NODE");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.println(indent + "ENTITY_REFERENCE_NODE");
        break;
    case Node.NOTATION_NODE:
        System.out.println(indent + "NOTATION_NODE");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
        break;
    case Node.TEXT_NODE:
        System.out.print(indent + "TEXT_NODE");
        System.out.println(" : " + node.getTextContent());
        break;
    default:
        System.out.println(indent + "Unknown node");
        break;
    }

    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        dumpLoop(list.item(i), indent + "   ");
    }
}

From source file:Main.java

/**
 * Extract nested text from a node. Currently does not handle coalescing
 * text nodes, CDATA sections, etc./*w w  w.j  a va 2  s  . com*/
 *
 * @param parent a parent element
 * @return the nested text, or null if none was found
 *
 * @since 8.4
 */
public static String findText(Node parent) {
    NodeList l = parent.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        if (l.item(i).getNodeType() == Node.TEXT_NODE) {
            Text text = (Text) l.item(i);
            return text.getNodeValue();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Returns element's TEXT child node (if it has one).
 * @param element the element whose TEXT we need to get.
 * @return a <tt>Text</tt> object containing the specified element's
 * text content.//from  w  w w .j  a  v  a  2 s.c o  m
 */
public static Text getTextNode(Element element) {
    return (Text) getChildByType(element, Node.TEXT_NODE);
}

From source file:de.betterform.connector.serializer.FormDataSerializer.java

/**
 * Serialize instance into multipart/form-data stream as defined in
 * http://www.w3.org/TR/xforms/slice11.html#serialize-form-data
 *
 * @param submission//from w w w  .j a  va2  s .  c  om
 * @param instance
 * @param wrapper
 * @param defaultEncoding
 * @throws Exception on error
 */
public void serialize(Submission submission, Node instance, SerializerRequestWrapper wrapper,
        String defaultEncoding) throws Exception {
    // sanity checks
    if (instance == null) {
        return;
    }

    switch (instance.getNodeType()) {

    case Node.ELEMENT_NODE:
    case Node.TEXT_NODE:
        break;

    case Node.DOCUMENT_NODE:
        instance = ((Document) instance).getDocumentElement();
        break;

    default:
        return;
    }

    String encoding = defaultEncoding;
    if (submission.getEncoding() != null) {
        encoding = submission.getEncoding();
    }

    // generate boundary
    Random rnd = new Random(System.currentTimeMillis());
    String boundary = DigestUtils.md5Hex(getClass().getName() + rnd.nextLong());

    // serialize the instance
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(bos, encoding)));
    if (instance.getNodeType() == Node.ELEMENT_NODE) {
        serializeElement(writer, (Element) instance, boundary, encoding);
    } else {
        writer.print(instance.getTextContent());
    }
    writer.print("\r\n--" + boundary + "--");
    writer.flush();
    bos.writeTo(wrapper.getBodyStream());
    wrapper.addHeader("internal-boundary-mark", boundary);
}

From source file:Main.java

/**
 * Retrieves the text of a given element.
 * //  ww  w.  ja v a2 s  .  com
 * @param elem the Element for which the text value is requested
 * @return the text value of that element or null if the element has no text value
 */
static public String getElementText(Node elem) {
    String value = null;
    Node node = (elem != null) ? elem.getFirstChild() : null;
    // Find Text
    while (node != null) { // Find all Text nodes
        if (node.getNodeType() == Node.TEXT_NODE) { // set or append
            if (value == null)
                value = node.getNodeValue();
            else
                value += node.getNodeValue();
        }
        node = node.getNextSibling();
    }
    return value;
}

From source file:Main.java

static final void getSetRec(final Node rootNode, final Set result, final Node exclude, final boolean com) {
    //Set result = new HashSet();
    if (rootNode == exclude) {
        return;//from www . ja  va  2  s.c  o  m
    }
    switch (rootNode.getNodeType()) {
    case Node.ELEMENT_NODE:
        result.add(rootNode);
        Element el = (Element) rootNode;
        if (el.hasAttributes()) {
            NamedNodeMap nl = ((Element) rootNode).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);
    }
    return;
}

From source file:Main.java

/**prints the type of the input node
 * @param node node to print type of//from  ww w  .ja va2s. c o m
 * @param ident amount to indent*/
public static void printNodeType(Node node, int ident) {
    System.out.print("Node: " + node.getNodeName() + " ");
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        System.out.println("Document Node");
        break;

    case Node.ELEMENT_NODE:
        System.out.println("Element Node");
        for (int j = 0; j < 2 * ident; j++)
            System.out.print(" ");
        System.out.println("It has the following Children");
        NodeList children = node.getChildNodes();
        if (children != null) {
            for (int i = 0; i < children.getLength(); i++) {
                for (int j = 0; j < ident; j++)
                    System.out.print(" ");
                System.out.print("Child " + ident + "." + i + " = ");
                printNodeType(children.item(i), ident + 1);
            }
            System.out.println();
        }
        break;

    case Node.TEXT_NODE:
        System.out.println("->" + node.getNodeValue().trim() + "<-");
        break;

    case Node.CDATA_SECTION_NODE:
        System.out.println("CData Node");
        break;

    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println("Proposing Instruction Node");
        break;

    case Node.ENTITY_REFERENCE_NODE:
        System.out.println("Entity Node");
        break;

    case Node.DOCUMENT_TYPE_NODE:
        System.out.println("Document Node");
        break;

    default:
    }
}

From source file:Main.java

/**
 * Looks for a text child node and returns its value.
 *
 * @param tag - XML element/*from   www  .  ja  v a 2  s . c  o m*/
 * @return - the text String of the tag
 */
public static String getText(final Element tag) {
    if (tag == null)
        return null;

    NodeList lst = tag.getChildNodes();
    StringBuffer buf = new StringBuffer();

    for (int Index = 0, Cnt = lst.getLength(); Index < Cnt; Index++) {
        if (lst.item(Index).getNodeType() == Node.ENTITY_REFERENCE_NODE) {
            buf.append(lst.item(Index).getChildNodes().item(0).getNodeValue());
        }
        if ((lst.item(Index).getNodeType() == Node.TEXT_NODE)
                || (lst.item(Index).getNodeType() == Node.CDATA_SECTION_NODE)) {
            buf.append(lst.item(Index).getNodeValue());
        }
    }

    if (buf.length() == 0)
        return null;
    else
        return buf.toString();
}

From source file:dk.statsbiblioteket.util.xml.DOM.java

/**
 * Extracts all textual and CDATA content from the given node and its
 * children./*w ww .j  a v a  2  s. c o  m*/
 *
 * @param node the node to get the content from.
 * @return the textual content of node.
 */
public static String getElementNodeValue(Node node) {
    StringWriter sw = new StringWriter(2000);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        NodeList all = node.getChildNodes();
        for (int i = 0; i < all.getLength(); i++) {
            if (all.item(i).getNodeType() == Node.TEXT_NODE
                    || all.item(i).getNodeType() == Node.CDATA_SECTION_NODE) {
                // TODO: Check if we exceed the limit for getNodeValue
                sw.append(all.item(i).getNodeValue());
            }
        }
    }
    return sw.toString();
}