Example usage for org.w3c.dom Node setNodeValue

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

Introduction

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

Prototype

public void setNodeValue(String nodeValue) throws DOMException;

Source Link

Document

The value of this node, depending on its type; see the table above.

Usage

From source file:Main.java

static public void SetNodeText(Node node, String value) {
    NodeList node_;/*ww w  .  j  a  v  a2  s.c o m*/
    Node x;
    int n, nnode;
    if (value == null)
        value = "";
    if (node.getNodeType() == Node.TEXT_NODE)
        node.setNodeValue(value);
    else {
        node_ = node.getChildNodes();
        nnode = node_.getLength();
        if (nnode == 0) {
            x = (Node) node.getOwnerDocument().createTextNode(value);
            node.appendChild(x);
        } else {
            for (n = 0; n < nnode; n++) {
                x = (Node) node_.item(n);
                if (x == null)
                    continue;
                if (x.getNodeType() == Node.TEXT_NODE) {
                    x.setNodeValue(value);
                    break;
                }
            }
        }
    }
}

From source file:Main.java

/**
 * Set or add node attribute./* w  ww .  j a va 2 s .co  m*/
 * 
 * @param node
 *          the node
 * @param attributeName
 *          the attribute name
 * @param attributeValue
 *          the attribute value
 * @param doc
 *          the document
 */
public static void setAddNodeAttribute(Node node, String attributeName, String attributeValue, Document doc) {
    if (null == node) {
        return;
    }
    NamedNodeMap attributes = node.getAttributes();
    Element element = (Element) node;
    if (null == attributes) {
        element.setAttributeNode(getAttribute(attributeName, attributeValue, doc));
    } else {
        Node n = attributes.getNamedItem(attributeName);
        if (null == n) {
            element.setAttributeNode(getAttribute(attributeName, attributeValue, doc));
        } else {
            n.setNodeValue(attributeValue);
        }
    }
}

From source file:Main.java

public static void setByPath(Node doc, String path, String value) {
    Node node = getNodeByPath(doc, path);
    if (node.hasChildNodes() && node.getFirstChild().getNodeType() == Node.TEXT_NODE) {
        node.getFirstChild().setTextContent(value);
    } else {/*from  w  w w.  j  a v a2  s.c o  m*/
        node.setNodeValue(value);
    }
}

From source file:Main.java

/**
 * Sets the Node n's text value to that of s.
 * @param Node - the node whose text will be set.
 * If that node isn't a text node. It will set the first childs that is a text node.
 * The rest will be ignored/* w w  w .  j  a v  a  2  s  . c  o m*/
 * @param String - s the text that will be set to the node.
 */
public static void setNodeText(Node n, String s) {
    if (n == null || s == null || s.length() == 0) {
        System.err.println("An argument is null");
        return;
    }

    if (n.getNodeType() == Node.TEXT_NODE) {
        n.setNodeValue(s);
        return;
    }

    NodeList nl = n.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node cn = nl.item(i);
        if (cn == null)
            continue;
        if (cn.getNodeType() == Node.TEXT_NODE) {
            cn.setNodeValue(s);
            break;
        }
    }
}

From source file:DomUtil.java

public static void setAttribute(Node node, String attName, String val) {
    NamedNodeMap attributes = node.getAttributes();
    Node attNode = node.getOwnerDocument().createAttribute(attName);
    attNode.setNodeValue(val);
    attributes.setNamedItem(attNode);//from w  w w  .  j  a  v a 2 s  .  c o  m
}

From source file:Main.java

/**
 * This method sets value to given node//from  w  w w.ja va  2s  .  c  o  m
 * 
 * @param inputNode
 *            Node to which value needs to be set
 * @param nodeValue
 *            Value to set
 * @throws IllegalArgumentException
 *             if input is invalid
 */
public static void setNodeTextValue(final Node inputNode, final String nodeValue)
        throws IllegalArgumentException {
    // Child list
    NodeList childList = null;

    // Validate input stream
    if (inputNode == null) {
        throw new IllegalArgumentException("Input Node cannot be null in XmlUtils.setNodeValue");
    }

    // Get child list
    childList = inputNode.getChildNodes();

    // If child nodes found
    if ((childList != null) && (childList.getLength() > 0)) {
        // Get child count
        final int childCount = childList.getLength();

        // For each child
        for (int childIndex = 0; childIndex < childCount; childIndex++) {
            final Node childNode = childList.item(childIndex);
            // Check if text node
            if ((childNode != null) && (childNode.getNodeType() == Node.TEXT_NODE)) {
                // Set value to text node
                childNode.setNodeValue(nodeValue);
                break;
            }
        }
    } else {
        // Create text node and set node value
        inputNode.appendChild(inputNode.getOwnerDocument().createTextNode(nodeValue));
    }
}

From source file:no.kantega.commons.util.XMLHelper.java

public static Element setChildText(Document doc, Element parent, String name, String value) {
    Element child = getChildByName(parent, name);
    if (child == null) {
        child = doc.createElement(name);
        child.appendChild(doc.createTextNode(value == null ? "" : value));
        parent.appendChild(child);//from   w w w .  j a v  a2 s.c  o  m
    } else {
        Node text = child.getFirstChild();
        if (text != null) {
            text.setNodeValue(value);
        } else {
            child.appendChild(doc.createTextNode(value == null ? "" : value));
        }
    }
    return child;
}

From source file:Main.java

/**
 *  Sets a named attribute of a Node//from www.j  av a 2s . c  om
 *  @param node The node
 *  @param attr The name of the attribute to set
 *  @param value The value to assign to the attribute
 */
public synchronized static void setAttribute(Node node, String attr, String value) {
    if (node == null)
        throw new IllegalArgumentException("Node argument cannot be null");
    if (attr == null)
        throw new IllegalArgumentException("Node attribute argument cannot be null");
    if (value == null)
        throw new IllegalArgumentException("Node attribute value argument cannot be null");

    Node attrN = null;
    NamedNodeMap map = node.getAttributes();
    if (map != null)
        attrN = map.getNamedItem(attr);

    if (attrN == null) {
        attrN = node.getOwnerDocument().createAttribute(attr);
        map.setNamedItem(attrN);
    }

    attrN.setNodeValue(value);
}

From source file:Main.java

/**
 * Sets the text value of an Element. if current text of the element is
 * replaced with the new text if text is null any
 * current text value is deleted./*from www .j  a v  a 2s  .co  m*/
 * 
 * @param elem the Element for which the text value should be set
 * @param text the new text value of the element
 * @return true if the text could be set or false otherwise
 */
static public boolean setElementText(Node elem, Object text) {
    if (elem == null)
        return false; // Fehler
    // Find Text
    Node node = elem.getFirstChild();
    while (node != null) { // Find all Text nodes
        if (node.getNodeType() == Node.TEXT_NODE)
            break; // gefunden
        node = node.getNextSibling();
    }
    if (node != null) { // Set or remove text
        if (text != null)
            node.setNodeValue(text.toString());
        else
            elem.removeChild(node);
    } else if (text != null) { // Add Text
        elem.appendChild(elem.getOwnerDocument().createTextNode(text.toString()));
    }
    return true;
}

From source file:de.decidr.model.workflowmodel.deployment.PackageBuilder.java

/**
 * Replaces all location attributes that match "unknow" with the new
 * endpoint/*w  ww . j  a v  a 2s. c o  m*/
 * 
 * @param service
 *            parsed wsdl
 * @param newEndpoint
 *            the new endpoint url
 */
private static void modifyServiceEndpoint(TDefinitions service, String newEndpoint) {
    List<TDocumented> topLevelList = service.getAnyTopLevelOptionalElement();
    List<TPort> portList = null;
    for (TDocumented node : topLevelList) {
        if (node instanceof TService) {
            TService serviceEntry = (TService) node;
            portList = serviceEntry.getPort();
        }
    }
    for (TPort port : portList) {
        for (Object portDOM : port.getAny()) {
            if (portDOM instanceof Node) {
                Node location = ((Node) portDOM).getAttributes().getNamedItem("location");
                if (location.getNodeValue().startsWith("unknown")) {
                    location.setNodeValue(newEndpoint);
                }
            }
        }
    }
}