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

/**
 * Convenience method to copy the contents of the old {@link Node} into the
 * new one./*  w w  w  .  ja v  a  2s  .  c  o  m*/
 * 
 * @param newDoc
 * @param newNode
 * @param oldParent
 */
public static void copyContents(Document newDoc, Node newNode, Node oldNode) {
    // FIXME we should be able to achieve this with much less code (e.g.
    // the code commented out) but for some reason there are
    // incompatibility issues being spat out by the tests.
    //      final NodeList childNodes = oldNode.getChildNodes();
    //        for (int i = 0; i < childNodes.getLength(); i++) {
    //           final Node child = newDoc.importNode(childNodes.item(i), true);
    //           newNode.appendChild(child);
    //        }

    final NodeList childs = oldNode.getChildNodes();

    for (int i = 0; i < childs.getLength(); i++) {
        final Node child = childs.item(i);
        Element newElem = null;
        switch (child.getNodeType()) {
        case Node.ELEMENT_NODE:
            //Get all the attributes of an element in a map
            final NamedNodeMap attrs = child.getAttributes();

            // Process each attribute
            newElem = newDoc.createElement(child.getNodeName());
            for (int j = 0; j < attrs.getLength(); j++) {
                final Attr attr = (Attr) attrs.item(j);
                // add attribute name and value to the new element
                newElem.setAttribute(attr.getNodeName(), attr.getNodeValue());
            }
            newNode.appendChild(newElem);
            break;

        case Node.TEXT_NODE:
            newNode.appendChild(newDoc.createTextNode(getString(child)));
        }
        copyContents(newDoc, newElem, child);
    }
}

From source file:Main.java

/**
 * Transform this {@link Node} into a {@link String} representation.
 *
 * @param xml/*  w w w  .j a v a 2s  .co m*/
 *        the xml Node
 * @return a String representation of the given xml Node
 */
public static String toString(Node xml) {
    short type = xml.getNodeType();

    if (type == Node.TEXT_NODE)
        return xml.getNodeValue();

    StringWriter buffer = new StringWriter();
    try {
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(new DOMSource(xml), new StreamResult(buffer));
    } catch (IllegalArgumentException | TransformerException e) {
        //throw new XMLHandlingException("Cannot create String from Node '" + xml + "'.");
        return "";
    }
    return buffer.toString();
}

From source file:Main.java

/**
 * returns the Node Type As String//  w  w  w  .ja v a2 s  . c o  m
 * @param node
 * @param cftype 
 * @return
 */
public static String getTypeAsString(Node node, boolean cftype) {
    String suffix = cftype ? "" : "_NODE";

    switch (node.getNodeType()) {
    case Node.ATTRIBUTE_NODE:
        return "ATTRIBUTE" + suffix;
    case Node.CDATA_SECTION_NODE:
        return "CDATA_SECTION" + suffix;
    case Node.COMMENT_NODE:
        return "COMMENT" + suffix;
    case Node.DOCUMENT_FRAGMENT_NODE:
        return "DOCUMENT_FRAGMENT" + suffix;
    case Node.DOCUMENT_NODE:
        return "DOCUMENT" + suffix;
    case Node.DOCUMENT_TYPE_NODE:
        return "DOCUMENT_TYPE" + suffix;
    case Node.ELEMENT_NODE:
        return "ELEMENT" + suffix;
    case Node.ENTITY_NODE:
        return "ENTITY" + suffix;
    case Node.ENTITY_REFERENCE_NODE:
        return "ENTITY_REFERENCE" + suffix;
    case Node.NOTATION_NODE:
        return "NOTATION" + suffix;
    case Node.PROCESSING_INSTRUCTION_NODE:
        return "PROCESSING_INSTRUCTION" + suffix;
    case Node.TEXT_NODE:
        return "TEXT" + suffix;
    default:
        return "UNKNOW" + suffix;
    }
}

From source file:Main.java

/**
 * getNodeText//from  www  .  ja  va2s  .  com
 * @param n node
 * @return text node content
 */
public static String getNodeText(Node n) {
    NodeList nl = n.getChildNodes();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < nl.getLength(); i++) {
        Node cn = nl.item(i);
        if (cn.getNodeType() == Node.TEXT_NODE) {
            Text txt = (Text) cn;
            sb.append(txt.getData().trim());
        }
    }

    return sb.toString();
}

From source file:Main.java

public static Object getContent(Element element) {
    NodeList nl = element.getChildNodes();
    StringBuilder content = new StringBuilder();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);//from   w w  w. j  a  va  2  s .c  o m
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            return node;
        case Node.CDATA_SECTION_NODE:
        case Node.TEXT_NODE:
            content.append(node.getNodeValue());
            break;
        }
    }
    return content.toString().trim();
}

From source file:Main.java

/** Get string value of an element.
 *  @param element Element//from  w ww .  j  a va 2  s  .  c  om
 *  @return String of the node. Empty string if nothing found.
 */
public static String getString(final Element element) {
    final Node text = element.getFirstChild();
    if (text == null) // <empty /> node
        return "";
    if ((text.getNodeType() == Node.TEXT_NODE || text.getNodeType() == Node.CDATA_SECTION_NODE))
        return text.getNodeValue();
    return "";
}

From source file:Main.java

public static String getNodeValue(String tagName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            NodeList childNodes = node.getChildNodes();
            for (int y = 0; y < childNodes.getLength(); y++) {
                Node data = childNodes.item(y);
                if (data.getNodeType() == Node.TEXT_NODE)
                    return data.getNodeValue();
            }/* w w w  . jav  a 2  s  .c o m*/
        }
    }
    return "";
}

From source file:DocWriter.java

private static void serializeNode(Node node, Writer out, String indent) throws IOException {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        out.write("<?xml version=\"1.0\"?>" + '\n');
        writeNode(((Document) node).getDocumentElement(), out, indent);
        break;//from w  ww  .j  a  va2s  .  c  om
    case Node.ELEMENT_NODE:
        writeNode(node, out, indent);
        break;
    case Node.TEXT_NODE:
        out.write(node.getNodeValue());
        break;
    case Node.COMMENT_NODE:
        out.write( /*indent + */ "<!--" + node.getNodeValue() + "-->");
        break;
    default:
        System.out.println("Got node: " + node.getNodeName());
        break;
    }
}

From source file:Main.java

/**
 * Returns the text content of the provided element as is.  If multiple text
 * DOM nodes are present, they are concatenated together. 
 * /*from   ww  w . ja  v  a2s.  c om*/
 * @param element The element that contains the desired text content.
 * @return The text content of the given element.
 * @throws Exception If an exception occurs during the traversal of the DOM
 * objects.
 */
public static String getElementTextData(Element element) throws Exception {
    if (!element.hasChildNodes()) {
        throw new Exception("Element has no children.");
    }

    Node n = element.getFirstChild();

    if ((n.getNodeType() != Node.TEXT_NODE) && (n.getNodeType() != Node.CDATA_SECTION_NODE)) {
        // must be a textual node
        // for now throw an exception, but later need to just skip this module and
        // resume initialization
        throw new Exception("Element child node is not textual.");
    }

    CharacterData value = (CharacterData) n;
    return value.getData();
}

From source file:Main.java

/**
 * Get the content of the given element.
 *
 * @param element   The element to get the content for.
 * @param defaultStr The default to return when there is no content.
 * @return The content of the element or the default.
 *///w  w w  .j av a 2 s .co  m
public static String getElementContent(Element element, String defaultStr) throws Exception {
    if (element == null) {
        return defaultStr;
    }

    NodeList children = element.getChildNodes();
    StringBuilder result = new StringBuilder("");
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i).getNodeType() == Node.TEXT_NODE
                || children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) {
            result.append(children.item(i).getNodeValue());
        }
        //         else if ( children.item( i ).getNodeType() == Node.COMMENT_NODE ) {
        //            // Ignore comment nodes
        //         }
    }
    return result.toString().trim();
}