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

public static void addElementToXml(String xmlFile, String nodeToAdd, String nodeContent)
        throws ParserConfigurationException, SAXException, IOException, TransformerException {

    Document doc = getXmlDoc(xmlFile);

    NodeList nodes = doc.getChildNodes();

    Node node = doc.createElement(nodeToAdd);
    node.setNodeValue(nodeContent);

    nodes.item(0).appendChild(node);/*w ww  . j a v a  2  s.  co m*/

    saveXml(doc, xmlFile);
}

From source file:Main.java

public static void setAttributeValue(Node node, String attribute, String value) {
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        Node nameAttr = attributes.getNamedItem(attribute);
        if (nameAttr != null) {
            nameAttr.setNodeValue(value);
        }//from www  .  jav  a  2 s .c o  m
    }
}

From source file:Main.java

/**
 * Convenience method for setting a node's (attr or elem) text value.
 * //from  w  ww.  j  ava  2  s .  c  om
 * @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 setNodeValue(Node node, String name, String value) {
    String s = node.getNodeValue();
    if (s != null) {
        node.setNodeValue(value);
        return;//from  ww w  .j a  v a 2 s. co m
    }
    NodeList nodelist = node.getChildNodes();
    for (int i = 0; i < nodelist.getLength(); i++) {
        if (nodelist.item(i) instanceof Text) {
            Text text = (Text) nodelist.item(i);
            text.setData(value);
            return;
        }
    }
    return;
}

From source file:Main.java

public static void incrementAttributeValue(Node node, String attName, int value) {
    Node attr = node.getAttributes().getNamedItem(attName);
    String attValue = String.valueOf(Integer.parseInt(attr.getNodeValue()) + value);
    attr.setNodeValue(attValue);
}

From source file:Main.java

/**
 * Set node's attribute./*from  w  w  w. ja va 2s  . co m*/
 * 
 * @param node
 *          the node
 * @param attributeName
 *          the attribute name
 * @param value
 *          the value
 */
public static void setNodeAttributeValue(Node node, String attributeName, String value) {
    if (null == node) {
        // Do Nothing
        return;
    }
    NamedNodeMap attributes = node.getAttributes();
    if (null == attributes) {
        // Do Nothing
        return;
    }
    Node n = attributes.getNamedItem(attributeName);
    if (null == n) {
        // Do Nothing
        return;
    }
    n.setNodeValue(value);
}

From source file:Main.java

/**
   Set the text of a specified node. A text section is added to
   the node if necessary.//  w  ww  .  j a va2 s . co  m
 */
static public void setText(Node node, String value) {
    if (node == null)
        return;

    Node child = findChild(node, "#text");
    if (child == null)
        node.appendChild(node.getOwnerDocument().createTextNode(value));
    else
        child.setNodeValue(value);
}

From source file:Main.java

/**
 * Updates the specified the child node/*from w  w w. java 2 s .  c  o m*/
 *  
 * @param parent   the parent node
 * @param nodeName   the name of child node to update
 * @param nodeValue   the new value
 * @return boolean  true 
 * @throws Exception
 */
public static boolean updateChildNodeValueByName(Document doc, Node parent, String nodeName, String nodeValue)
        throws Exception {
    Node node = findChildNodeByName(parent, nodeName);
    if (node == null)
        return false;

    Node firstNode = node.getFirstChild();
    if (firstNode == null) {
        node.appendChild(doc.createTextNode(nodeValue));
    } else {
        firstNode.setNodeValue(nodeValue);
    }
    return true;

}

From source file:Main.java

/***************************************************************************
 * Sets the value of the first child under the given element with the given
 * name. If the child has no nodes, one is created.
 * /* www .ja v  a2s. co  m*/
 * @param doc
 * @param e
 * @param name
 * @param value
 * @return
 * @throws Exception
 **************************************************************************/
public static boolean setChildValueByName(Document doc, Element e, String name, String value) throws Exception {
    NodeList childNodes = e.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        if (node.getNodeName().equals(name)) {
            Node child = node.getFirstChild();
            if (child == null)
                ((Element) node).appendChild(doc.createTextNode(value));
            else
                child.setNodeValue(value);

            return true;
        }
    }

    return false;
}

From source file:Main.java

public static void setNodeAttribute(Document doc, Node n, String attrName, String value) {
    Node namedAttr = n.getAttributes().getNamedItem(attrName);
    if (namedAttr == null) {
        namedAttr = doc.createAttribute(attrName);
        n.getAttributes().setNamedItem(namedAttr);
    }/*from   w  w w  .j  ava  2s .  co m*/
    namedAttr.setNodeValue(value);
}