Example usage for org.w3c.dom Node setTextContent

List of usage examples for org.w3c.dom Node setTextContent

Introduction

In this page you can find the example usage for org.w3c.dom Node setTextContent.

Prototype

public void setTextContent(String textContent) throws DOMException;

Source Link

Document

This attribute returns the text content of this node and its descendants.

Usage

From source file:Main.java

/**
 * Recursively removes all text nodes containing whitespace only from a document element. Also
 * trims leading and trailing whitespace from text nodes.
 * //from   w  w w.  j a v  a  2  s  .  c  om
 * @param e The root document element.
 */
public static void removeWhitespaceNodes(Element e) {
    NodeList children = e.getChildNodes();
    for (int i = children.getLength() - 1; i >= 0; i--) {
        Node child = children.item(i);
        if (child instanceof Text && ((Text) child).getData().trim().length() == 0)
            e.removeChild(child);
        else if (child instanceof Text)
            child.setTextContent(((Text) child).getData().trim());
        else if (child instanceof Element)
            removeWhitespaceNodes((Element) child);
    }
}

From source file:Main.java

/**
 * Convenience method for setting a node's (attr or elem) text value.
 * /*from   w  w w . j  a  va  2 s  . c  o m*/
 * @param node     The DOM node whose value needs to be changed.
 * @param newValue The value to set for node.
 */
public static void setNodeValue(final Node node, final String newValue) {
    short nodeType = node.getNodeType();
    if (nodeType == Node.ATTRIBUTE_NODE) {
        node.setNodeValue(newValue);
    } else if (nodeType == Node.ELEMENT_NODE) {
        node.setTextContent(newValue);
    }
}

From source file:Main.java

public static void updateNodeToXml(String nodeXpathStr, String xmlFilePath, String value,
        Map<String, String> attr) throws Exception {
    Document doc = null;//from   w  ww . j a  v  a  2s.c  o m
    if (xmlFilePath == null || nodeXpathStr == null)
        throw new Exception("some parameters can not be null!");
    doc = dombuilder.parse(new File(xmlFilePath));
    Node pNode = (Node) xpath.compile(nodeXpathStr).evaluate(doc, XPathConstants.NODE);
    if (pNode == null)
        throw new Exception("can not find the node specified in nodeXpathStr!");
    ;
    pNode.setTextContent(value);

    if (attr != null && !attr.isEmpty()) {
        for (String key : attr.keySet())
            ((Element) pNode).setAttribute(key, attr.get(key));
    }

    writeToXmlFile(doc, xmlFilePath);
}

From source file:Main.java

public static void processNode(Node node) throws Exception {
    if (node.hasChildNodes()) {
        for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
            processNode(child);/*from   w w  w.j av  a  2s  .  co m*/
        }
    } else {
        node.setTextContent(" ");
    }
}

From source file:de.ingrid.iplug.csw.dsc.TestUtil.java

public static void setRecordTitle(CSWRecord record, String title) {
    Node titleNode = xPathUtils.getNode(record.getOriginalResponse(), "//gmd:title/gco:CharacterString");
    titleNode.setTextContent(title);
}

From source file:Main.java

private static Element addNewProfileSection(Document document, Element profiles, String profileIdVal) {
    //start with profile node
    Element profile = document.createElement(PROFILE_NODE_NAME);
    profiles.appendChild(profile);//w  w w .j  a va 2  s .c  om

    //create the id tag
    Node id = document.createElement(PROFILE_ID_NAME);
    //id.setNodeValue(profileIdVal);
    id.setTextContent(profileIdVal);
    //add it to the profile
    profile.appendChild(id);

    //create activation tag
    Node activeByDefault = document.createElement(ACTIVEBYDEFAULT_NAME);
    //activeByDefault.setNodeValue(ACTIVEBYDEFAULT_VAL);
    activeByDefault.setTextContent(ACTIVEBYDEFAULT_VAL);
    Node activation = document.createElement(ACTIVATION_NAME);
    activation.appendChild(activeByDefault);
    //add it to profile
    profile.appendChild(activation);

    //create build tag
    Node build = document.createElement(BUILD_NAME);
    //add it to profile
    profile.appendChild(build);

    //create plugins tag, add to build
    Node plugins = document.createElement(PLUGINS_NAME);
    build.appendChild(plugins);

    return (Element) plugins;
}

From source file:it.polimi.diceH2020.plugin.control.JSonReader.java

/**
 * set the attribute named as "name" in the node "n" with a given value
 * //from   www  .  jav  a 2  s  .  co  m
 * @param n
 *            the node in which you want to set the attribute
 * @param name
 *            the name of the attribute
 * @param value
 *            the value of the attribute
 */
private static void setAttribute(Node n, String name, String value) {
    NamedNodeMap attributes = n.getAttributes();
    if (!JSonReader.findAttribute(attributes, name)) {
        Element el = (Element) n;
        el.setAttribute(name, value);
    } else {
        Node nodeAttrType1 = attributes.getNamedItem(name);
        nodeAttrType1.setTextContent(value);
    }
}

From source file:Main.java

public static NodeList ChangeNode(NodeList listNode, ArrayList<String> arrStrCompare, String strNewValue) {
    ArrayList<String> arrTempList = arrStrCompare;
    Iterator<String> iterator = arrTempList.iterator();

    while (iterator.hasNext()) {
        String strCompare = (String) iterator.next();
        iterator.remove();/*from ww w  .jav a 2  s.  com*/

        for (int i = 0; i < listNode.getLength(); i++) {
            Node node = listNode.item(i);

            if (strCompare.equals(node.getNodeName())) {
                if (iterator.hasNext()) {
                    node = ChangeNode(node.getChildNodes(), arrTempList, strNewValue).item(0);
                    break;
                }
                node.setTextContent(strNewValue);

                break;
            }
        }
    }

    return listNode;
}

From source file:com.ephesoft.dcma.util.OCREngineUtil.java

private static void setWordNodeTextContent(XPathExpression xOcrWordExpr, XPathExpression ocrXWordExpr,
        NodeList wordList, int wordNodeIndex) throws XPathExpressionException {
    Node wordNode = wordList.item(wordNodeIndex);
    if (wordNode != null) {
        Node word = (Node) xOcrWordExpr.evaluate(wordNode, XPathConstants.NODE);
        if (word != null) {
            wordNode.setTextContent(word.getTextContent());
        } else {/* w w  w  . ja v  a  2s . c  o m*/
            word = (Node) ocrXWordExpr.evaluate(wordNode, XPathConstants.NODE);
            if (word != null) {
                wordNode.setTextContent(word.getTextContent());
            }
        }
    }
}

From source file:com.amalto.core.storage.hibernate.DefaultStorageClassLoader.java

private static void setPropertyValue(Document document, String propertyName, String value)
        throws XPathExpressionException {
    XPathExpression compile = pathFactory
            .compile("hibernate-configuration/session-factory/property[@name='" + propertyName + "']"); //$NON-NLS-1$ //$NON-NLS-2$
    Node node = (Node) compile.evaluate(document, XPathConstants.NODE);
    if (node != null) {
        node.setTextContent(value);
    } else {//from   w ww.jav  a2  s  . co  m
        XPathExpression parentNodeExpression = pathFactory.compile("hibernate-configuration/session-factory"); //$NON-NLS-1$
        Node parentNode = (Node) parentNodeExpression.evaluate(document, XPathConstants.NODE);
        // Create a new property element for this datasource-specified property (TMDM-4927).
        Element property = document.createElement("property"); //$NON-NLS-1$
        Attr propertyNameAttribute = document.createAttribute("name"); //$NON-NLS-1$
        property.setAttributeNode(propertyNameAttribute);
        propertyNameAttribute.setValue(propertyName);
        property.setTextContent(value);
        parentNode.appendChild(property);
    }
}