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:com.runwaysdk.request.ClientRequestManager.java

/**
 * Parses an XML document to extract connection information, ultimately creating Connection objects.
 * /*from w w w  . ja  v a  2  s  .  c  o  m*/
 * @param document
 */
private void parseDocument(Document document) {
    NodeList connectionsList = document.getElementsByTagName(CONNECTION_ELEMENT);

    // go through each connection
    for (int i = 0; i < connectionsList.getLength(); i++) {
        Node connection = connectionsList.item(i);
        NodeList connectionData = connection.getChildNodes();

        // get the data for each connection
        String label = null;
        ConnectionLabel.Type type = null;
        String address = null;

        // we have to loop through all child nodes since whitespace
        // counts as a text node
        for (int j = 0; j < connectionData.getLength(); j++) {
            Node data = connectionData.item(j);

            // ignore all \n\t text nodes
            if (data.getNodeType() == Node.TEXT_NODE) {
                continue;
            }

            if (data.getNodeName().equals(LABEL_ELEMENT)) {
                label = data.getTextContent();
            } else if (data.getNodeName().equals(TYPE_ELEMENT)) {
                String typeValue = data.getTextContent();
                type = ConnectionLabel.Type.dereference(typeValue);
            } else if (data.getNodeName().equals(ADDRESS_ELEMENT)) {
                address = data.getTextContent();
            }
        }

        connections.put(label, new ConnectionLabel(label, type, address));
    }
}

From source file:Main.java

/**
 * Generates XPath expression with the option of the Node values appended
 *
 * @param node             the Node whose XPath is to be found
 * @param parentXPath      the XPath of the parent Node
 * @param ignoreWhitespace the flag to indicate if Whitespace will be ignored
 * @param includeValues    the flag to indicate if Node values will be included
 * @param noIndex          the flag to indicate if Node indexes are included
 * @return the XPath string representation of the Node
 *//*  ww  w. j av  a2  s  .  c  o m*/

public static String generateXPath(Node node, String parentXPath, boolean ignoreWhitespace,
        boolean includeValues, boolean noIndex)

{

    boolean noValues = !includeValues;

    if (node == null)

        return "";

    Node parent = node.getParentNode();

    int index = noIndex ? 0 : getXPathNodeIndex(node, ignoreWhitespace);

    String indexStr = "";

    if (index > 0)

        indexStr = "[" + Integer.toString(index) + "]";

    if (node.getNodeType() == Node.DOCUMENT_NODE)

    {

        // return only the blank String, since all the other types are preceded with /

        return parentXPath + "";

    } else if (node.getNodeType() == Node.TEXT_NODE)

    {

        return parentXPath +

                (noValues ? "/" + node.getNodeValue() + indexStr
                        : "/TEXT(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.ELEMENT_NODE)

    {

        return parentXPath +

                "/" + node.getNodeName() + indexStr;

    } else if (node.getNodeType() == Node.COMMENT_NODE)

    {

        return parentXPath +

                (noValues ? "/" + node.getNodeValue() + indexStr
                        : "/COMMENT(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.ENTITY_REFERENCE_NODE)

    {

        return parentXPath +

                (noValues ? "/" + node.getNodeValue() + indexStr
                        : "/EntityReference(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE)

    {

        return parentXPath +

                (noValues ? "/" + node.getNodeValue() + indexStr
                        : "/PI(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE)

    {

        return parentXPath + "/[@" + node.getNodeName() +

                (noValues ? "" : "=" + node.getNodeValue()) + "]";

    } else if (node.getNodeType() == Node.DOCUMENT_TYPE_NODE)

    {

        return parentXPath +

                (noValues ? "/" + node.getNodeValue() : "/DOCTYPE(" + node.getNodeName() + ")");

    } else if (node.getNodeType() == Node.CDATA_SECTION_NODE)

    {

        return parentXPath +

                (noValues ? "/" + node.getNodeValue() : "/CDATA(" + node.getNodeName() + ")");

    }

    // Wont reach this far but just in case

    return "";

}

From source file:com.connectutb.xfuel.FuelPlanner.java

public final String getElementValue(Node elem) {
    Node child;/*from  w w w  .jav  a  2  s.co m*/
    if (elem != null) {
        if (elem.hasChildNodes()) {
            for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
                if (child.getNodeType() == Node.TEXT_NODE) {
                    return child.getNodeValue();
                }
            }
        }
    }
    return "";
}

From source file:eu.planets_project.services.utils.cli.CliMigrationPaths.java

private static String decodeCommandNode(Node command) {
    Node commandtext = command.getFirstChild();
    if (commandtext.getNodeType() == Node.TEXT_NODE) {
        return commandtext.getNodeValue();
    }/*w w  w  . j a  v  a 2s.c om*/
    return "";
}

From source file:DomPrintUtil.java

/**
 * Returns XML text converted from the target DOM
 * //from  w w w  . ja  v  a2  s. co  m
 * @return String format XML converted from the target DOM
 */
public String toXMLString() {
    StringBuffer tmpSB = new StringBuffer(8192);

    TreeWalkerImpl treeWalker = new TreeWalkerImpl(document, whatToShow, nodeFilter, entityReferenceExpansion);

    String lt = escapeTagBracket ? ESC_LT : LT;
    String gt = escapeTagBracket ? ESC_GT : GT;
    String line_sep = indent ? LINE_SEP : EMPTY_STR;

    Node tmpN = treeWalker.nextNode();
    boolean prevIsText = false;

    String indentS = EMPTY_STR;
    while (tmpN != null) {
        short type = tmpN.getNodeType();
        switch (type) {
        case Node.ELEMENT_NODE:
            if (prevIsText) {
                tmpSB.append(line_sep);
            }
            tmpSB.append(indentS + lt + tmpN.getNodeName());
            NamedNodeMap attrs = tmpN.getAttributes();
            int len = attrs.getLength();
            for (int i = 0; i < len; i++) {
                Node attr = attrs.item(i);
                String value = attr.getNodeValue();
                if (null != value) {
                    tmpSB.append(getAttributeString((Element) tmpN, attr));
                }
            }
            if (tmpN instanceof HTMLTitleElement && !tmpN.hasChildNodes()) {
                tmpSB.append(gt + ((HTMLTitleElement) tmpN).getText());
                prevIsText = true;
            } else if (checkNewLine(tmpN)) {
                tmpSB.append(gt + line_sep);
                prevIsText = false;
            } else {
                tmpSB.append(gt);
                prevIsText = true;
            }
            break;
        case Node.TEXT_NODE:
            if (!prevIsText) {
                tmpSB.append(indentS);
            }
            tmpSB.append(getXMLString(tmpN.getNodeValue()));
            prevIsText = true;
            break;
        case Node.COMMENT_NODE:
            String comment;
            if (escapeTagBracket) {
                comment = getXMLString(tmpN.getNodeValue());
            } else {
                comment = tmpN.getNodeValue();
            }
            tmpSB.append(line_sep + indentS + lt + "!--" + comment + "--" + gt + line_sep);
            prevIsText = false;
            break;
        case Node.CDATA_SECTION_NODE:
            tmpSB.append(line_sep + indentS + lt + "!CDATA[" + tmpN.getNodeValue() + "]]" + line_sep);
            break;
        case Node.DOCUMENT_TYPE_NODE:
            if (tmpN instanceof DocumentType) {
                DocumentType docType = (DocumentType) tmpN;
                String pubId = docType.getPublicId();
                String sysId = docType.getSystemId();
                if (null != pubId && pubId.length() > 0) {
                    if (null != sysId && sysId.length() > 0) {
                        tmpSB.append(lt + "!DOCTYPE " + docType.getName() + " PUBLIC \"" + pubId + " \"" + sysId
                                + "\">" + line_sep);
                    } else {
                        tmpSB.append(
                                lt + "!DOCTYPE " + docType.getName() + " PUBLIC \"" + pubId + "\">" + line_sep);
                    }
                } else {
                    tmpSB.append(lt + "!DOCTYPE " + docType.getName() + " SYSTEM \"" + docType.getSystemId()
                            + "\">" + line_sep);

                }
            } else {
                System.out.println("Document Type node does not implement DocumentType: " + tmpN);
            }
            break;
        default:
            System.out.println(tmpN.getNodeType() + " : " + tmpN.getNodeName());
        }

        Node next = treeWalker.firstChild();
        if (null != next) {
            if (indent && type == Node.ELEMENT_NODE) {
                indentS = indentS + " ";
            }
            tmpN = next;
            continue;
        }

        if (tmpN.getNodeType() == Node.ELEMENT_NODE) {
            tmpSB.append(lt + "/" + tmpN.getNodeName() + gt + line_sep);
            prevIsText = false;
        }

        next = treeWalker.nextSibling();
        if (null != next) {
            tmpN = next;
            continue;
        }

        tmpN = null;
        next = treeWalker.parentNode();
        while (null != next) {
            if (next.getNodeType() == Node.ELEMENT_NODE) {
                if (indent) {
                    if (indentS.length() > 0) {
                        indentS = indentS.substring(1);
                    } else {
                        System.err.println("indent: " + next.getNodeName() + " " + next);
                    }
                }
                tmpSB.append(line_sep + indentS + lt + "/" + next.getNodeName() + gt + line_sep);
                prevIsText = false;
            }
            next = treeWalker.nextSibling();
            if (null != next) {
                tmpN = next;
                break;
            }
            next = treeWalker.parentNode();
        }
    }
    return tmpSB.toString();
}

From source file:org.gvnix.dynamic.configuration.roo.addon.config.XmlDynamicConfiguration.java

/**
 * Generate a dynamic property list from a list of XML nodes.
 * <p>//  w  w  w  . j  ava2s.c om
 * Only TEXT_NODE, TEXT_NODE and ATTRIBUTE_NODE nodes are considered. On
 * ATTRIBUTE_NODE nodes references neither are considered.
 * </p>
 * 
 * @param baseName Parent node name of node list
 * @param nodes XML node list to convert
 * @return Dynamic property list
 */
protected DynPropertyList getProperties(String baseName, NodeList nodes) {

    DynPropertyList dynProps = new DynPropertyList();

    // Iterate all nodes on list
    for (int i = 0; i < nodes.getLength(); i++) {

        Node node = nodes.item(i);

        // Only consider element, text or attribute nodes
        if (isValidNode(node)) {

            // Generate the xpath expression that points to property
            String xpath = getPropertyXpath(baseName, nodes, i);

            // Add dynamic properties related to node attributes
            dynProps.addAll(getPropertyAttributes(node, xpath));

            // Add dynamic properties related to childs nodes and attrs
            dynProps.addAll(getProperties(xpath, node.getChildNodes()));

            // Add dynamic property related to this node
            String content = node.getTextContent();
            if (node.getNodeType() == Node.TEXT_NODE && content.trim().length() > 0) {

                dynProps.add(new DynProperty(baseName, content));
            }
        }
    }

    return dynProps;
}

From source file:ua.kiev.doctorvera.utils.SMSGateway.java

public static final String getElementValue(Node elem) {
    Node child;//from w  w w .  j a v a2  s.com
    if (elem != null) {
        if (elem.hasChildNodes()) {
            for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
                if (child.getNodeType() == Node.TEXT_NODE) {
                    return child.getNodeValue();
                }
            }
        }
    }
    return "";
}

From source file:edu.duke.cabig.c3pr.webservice.integration.XMLUtils.java

private static boolean isIgnorableWhitespace(Node node) {
    return node.getNodeType() == Node.TEXT_NODE && StringUtils.isBlank(node.getNodeValue());
}

From source file:com.sqewd.os.maracache.core.Config.java

private ConfigPath load(ConfigNode parent, Element elm) throws ConfigException {
    if (parent instanceof ConfigPath) {
        // Check if there are any attributes.
        // Attributes are treated as Value nodes.
        if (elm.hasAttributes()) {
            NamedNodeMap map = elm.getAttributes();
            if (map.getLength() > 0) {
                for (int ii = 0; ii < map.getLength(); ii++) {
                    Node n = map.item(ii);
                    ((ConfigPath) parent).addValueNode(n.getNodeName(), n.getNodeValue());
                }//from www.  ja  v  a 2 s  .  c  om
            }
        }
        if (elm.hasChildNodes()) {
            NodeList children = elm.getChildNodes();
            for (int ii = 0; ii < children.getLength(); ii++) {
                Node cn = children.item(ii);
                if (cn.getNodeType() == Node.ELEMENT_NODE) {
                    Element e = (Element) cn;
                    if (e.hasChildNodes()) {
                        int nc = 0;
                        for (int jj = 0; jj < e.getChildNodes().getLength(); jj++) {
                            Node ccn = e.getChildNodes().item(jj);
                            // Read the text node if there is any.
                            if (ccn.getNodeType() == Node.TEXT_NODE) {
                                String n = e.getNodeName();
                                String v = ccn.getNodeValue();
                                if (!StringUtils.isEmpty(v.trim()))
                                    ((ConfigPath) parent).addValueNode(n, v);
                                nc++;
                            }
                        }
                        // Make sure this in not a text only node.
                        if (e.getChildNodes().getLength() > nc) {
                            // Check if this is a parameter node. Parameters are treated differently.
                            if (e.getNodeName().compareToIgnoreCase(ConfigParams.NODE_NAME) == 0) {
                                ConfigParams cp = ((ConfigPath) parent).addParamNode();
                                setParams(cp, e);
                            } else {
                                ConfigPath cp = ((ConfigPath) parent).addPathNode(e.getNodeName());
                                load(cp, e);
                            }
                        }
                    }
                }
            }
        }
    }
    return (ConfigPath) parent;
}

From source file:com.nridge.core.base.std.XMLUtl.java

public static String getNodeStrValue(Node aNode) {
    int count;/*from  w  w  w  . jav a2 s . co  m*/
    Node textNode;
    NodeList nodeList;

    nodeList = aNode.getChildNodes();
    count = nodeList.getLength();

    for (int i = 0; i < count; i++) {
        textNode = nodeList.item(i);

        if (textNode.getNodeType() == Node.TEXT_NODE)
            return textNode.getNodeValue();
    }

    return StringUtils.EMPTY;
}