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

/** Searches for a given node and returns text associated with
  * it. This version does not recurse to the node's children.
  * @param node Node to search.//  w  w w .ja  v a  2  s  .c o  m
  * @return Text associated with the node, or null if none found.
  */
public String getNodeText(Node node) {
    // Look for text in child (text stored in its own node).
    NodeList children = node.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);

        if ((child.getNodeType() == Node.CDATA_SECTION_NODE) || (child.getNodeType() == Node.TEXT_NODE)) {
            return (child.getNodeValue());
        }
    }

    // If we get this far, no text was found.
    return null;
}

From source file:com.qcadoo.view.internal.xml.ViewDefinitionParserImpl.java

@Override
public String getStringNodeContent(final Node node) {
    NodeList childNodes = node.getChildNodes();
    StringBuilder contentSB = new StringBuilder();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if (child.getNodeType() == Node.CDATA_SECTION_NODE || child.getNodeType() == Node.TEXT_NODE) {
            contentSB.append(child.getNodeValue());
        }/*from w w  w. j a  v a 2 s  .c o  m*/
    }
    return contentSB.toString().trim();
}

From source file:de.elbe5.base.data.XmlData.java

public String getText(Element node) {
    if (node.hasChildNodes()) {
        Node child = node.getFirstChild();
        while (child != null) {
            if (child.getNodeType() == Node.TEXT_NODE) {
                return child.getNodeValue();
            }//from  w ww .j a  v a 2s  . c o  m
            child = child.getNextSibling();
        }
    }
    return "";
}

From source file:importer.handler.post.stages.Discriminator.java

/**
 * Get the next sibling that is an element
 * @param elem the element/* w w  w. j av  a 2 s  .  co  m*/
 * @param skipText if true skip text nodes to next element node
 * @return its next sibling of elem or null
 */
static Element nextSibling(Element elem, boolean skipText) {
    Node n = elem.getNextSibling();
    while (n != null) {
        if (!skipText && n.getNodeType() == Node.TEXT_NODE && !isWhitespace(n.getTextContent()))
            return null;
        else if (n.getNodeType() == Node.ELEMENT_NODE)
            return (Element) n;
        n = n.getNextSibling();
    }
    return null;
}

From source file:com.mingo.parser.xml.dom.QuerySetParser.java

private void parseBody(StringBuilder builder, Node node, QuerySet querySet) throws ParserException {
    if (node == null) {
        return;//w  w w .j  a v  a  2s.c  om
    }
    if (node.getNodeType() == Node.TEXT_NODE) {
        String body = node.getNodeValue();
        if (StringUtils.isNotBlank(body)) {
            builder.append(processString(body));
        }
    } else if (FRAGMENT.equals(node.getNodeName())) {
        applyFragment(builder, getFragmentRef(node), querySet);
    }
}

From source file:Main.java

private static Node replace2TextExceptTags(Document document, Node node, String... tags) {

    NodeList nodeList = node.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node subNode = nodeList.item(i);
        if (subNode.getNodeType() == Node.TEXT_NODE) {
            String text = ((Text) subNode).getTextContent();
            text = text.replaceAll("[\t\n]+", "").replaceAll(" +", " ");
            subNode.setTextContent(text);
            continue;
        }/*from  w ww . jav a 2 s .  c om*/
        if (subNode.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }

        String nodeName = subNode.getNodeName();
        boolean excepted = false;
        for (String tagName : tags) {
            if (tagName.equals(nodeName)) {
                excepted = true;
                replace2TextExceptTags(document, subNode, tags);
                break;
            }
        }

        if (excepted) {
            continue;
        }

        subNode = replace2TextExceptTags(document, subNode, tags);
        NodeList childList = subNode.getChildNodes();
        List<Node> tempList = new ArrayList<Node>();
        for (int j = 0; j < childList.getLength(); j++) {
            tempList.add(childList.item(j));
        }
        for (Node child : tempList) {
            node.insertBefore(child, subNode);
        }
        node.removeChild(subNode);
    }

    return node;
}

From source file:com.searchbox.collection.oppfin.IdealISTCollection.java

private void addField(String uid, Document document, FieldMap fields, Class<?> clazz, String key,
        String fieldName) {/*  w w  w .j  av  a 2s. c om*/
    XPath xPath = XPathFactory.newInstance().newXPath();
    String metaDataExpression = "//metadata[@key='" + key + "']|" + "//content[@cid='" + key + "']";

    // read a nodelist using xpath
    NodeList nodeList;
    try {
        nodeList = (NodeList) xPath.compile(metaDataExpression).evaluate(document, XPathConstants.NODESET);

        for (int i = 0; i < nodeList.getLength(); i++) {

            StringBuilder textBuilder = new StringBuilder();
            for (int j = 0; j < nodeList.item(i).getChildNodes().getLength(); j++) {
                Node textNode = nodeList.item(i).getChildNodes().item(j);
                if (textNode.getNodeType() == Node.TEXT_NODE
                        || textNode.getNodeType() == Node.CDATA_SECTION_NODE) {
                    textBuilder.append(textNode.getNodeValue());
                }
            }
            String value = textBuilder.toString();

            LOGGER.debug("value for key {} is {}", key, value);
            if (value.isEmpty()) {
                continue;
            }
            if (String.class.isAssignableFrom(clazz)) {
                String content = new HtmlToPlainText().getPlainText(Jsoup.parse(value));
                fields.put(fieldName, content);
                fields.put(fieldName + "Html", value);
            } else if (Date.class.isAssignableFrom(clazz)) {
                Date date;
                try {
                    date = dfmt.parse(value);
                    fields.put(fieldName, date);
                } catch (ParseException e) {
                    LOGGER.warn("Could not parse date({}) for for key {} in document {}", value, key, uid);
                }
            }
        }
    } catch (XPathExpressionException e) {
        LOGGER.warn("Could not execute XPATH for key {} in document {}", key, uid);
    }
}

From source file:com.gargoylesoftware.htmlunit.xml.XmlUtil.java

/**
 * Copy all children from 'source' to 'dest', within the context of the specified page.
 * @param page the page which the nodes belong to
 * @param source the node to copy from//from   w ww.  ja va2  s.c o m
 * @param dest the node to copy to
 * @param handleXHTMLAsHTML if true elements from the XHTML namespace are handled as HTML elements instead of
 *     DOM elements
 */
private static void copy(final SgmlPage page, final Node source, final DomNode dest,
        final boolean handleXHTMLAsHTML) {
    final NodeList nodeChildren = source.getChildNodes();
    for (int i = 0; i < nodeChildren.getLength(); i++) {
        final Node child = nodeChildren.item(i);
        switch (child.getNodeType()) {
        case Node.ELEMENT_NODE:
            final DomNode childXml = createFrom(page, child, handleXHTMLAsHTML);
            dest.appendChild(childXml);
            copy(page, child, childXml, handleXHTMLAsHTML);
            break;

        case Node.TEXT_NODE:
            dest.appendChild(new DomText(page, child.getNodeValue()));
            break;

        case Node.CDATA_SECTION_NODE:
            dest.appendChild(new DomCDataSection(page, child.getNodeValue()));
            break;

        case Node.COMMENT_NODE:
            dest.appendChild(new DomComment(page, child.getNodeValue()));
            break;

        case Node.PROCESSING_INSTRUCTION_NODE:
            dest.appendChild(new DomProcessingInstruction(page, child.getNodeName(), child.getNodeValue()));
            break;

        default:
            LOG.warn(
                    "NodeType " + child.getNodeType() + " (" + child.getNodeName() + ") is not yet supported.");
        }
    }
}

From source file:com.hp.test.framework.generatejellytess.GenerateJellyTests.java

public static void getData(Node parentNode, Node node) {

    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE: {
        if (node.toString().contains("Verification")) {
            System.out.println("******" + node.toString());
            verfiy = true;/* w  w  w  . ja  va 2  s . c o  m*/
        }
        if (node.hasChildNodes()) {
            NodeList list = node.getChildNodes();
            int size = list.getLength();

            for (int index = 0; index < size; index++) {
                getData(node, list.item(index));
            }
        }

        break;
    }

    case Node.TEXT_NODE: {
        String data = node.getNodeValue();

        if (data.trim().length() > 0) {
            Locators.put(parentNode.getNodeName(), node.getNodeValue());
        }
        break;
    }

    }
}

From source file:com.dinochiesa.edgecallouts.EditXmlNode.java

private void execute0(Document document, MessageContext msgCtxt) throws Exception {
    String xpath = getXpath(msgCtxt);
    XPathEvaluator xpe = getXpe(msgCtxt);
    NodeList nodes = (NodeList) xpe.evaluate(xpath, document, XPathConstants.NODESET);
    validate(nodes);//w  ww .j ava  2  s .  co m
    EditAction action = getAction(msgCtxt);
    if (action == EditAction.Remove) {
        remove(nodes);
        return;
    }

    short newNodeType = getNewNodeType(msgCtxt);
    String text = getNewNodeText(msgCtxt);
    Node newNode = null;
    switch (newNodeType) {
    case Node.ELEMENT_NODE:
        // Create a duplicate node and transfer ownership of the
        // new node into the destination document.
        Document temp = XmlUtils.parseXml(text);
        newNode = document.importNode(temp.getDocumentElement(), true);
        break;
    case Node.ATTRIBUTE_NODE:
        if (text.indexOf("=") < 1) {
            throw new IllegalStateException("attribute spec must be name=value");
        }
        String[] parts = text.split("=", 2);
        if (parts.length != 2)
            throw new IllegalStateException("attribute spec must be name=value");
        Attr attr = document.createAttribute(parts[0]);
        attr.setValue(parts[1]);
        newNode = attr;
        break;
    case Node.TEXT_NODE:
        newNode = document.createTextNode(text);
        break;
    }
    switch (action) {
    case InsertBefore:
        insertBefore(nodes, newNode, newNodeType);
        break;
    case Append:
        append(nodes, newNode, newNodeType);
        break;
    case Replace:
        replace(nodes, newNode, newNodeType);
        break;
    }
}