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 main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new File("path/to/file.xml"));

    XPathFactory xFactory = XPathFactory.newInstance();
    XPath xPath = xFactory.newXPath();
    XPathExpression expression = xPath.compile("PersonList/Person/Age/text() | PersonList/Person/Name/text()");

    NodeList nodes = (NodeList) expression.evaluate(doc, XPathConstants.NODESET);
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getParentNode().getNodeName().equals("Name")) {
            node.setNodeValue("new name");
        } else {//  w w  w . j av  a 2  s.  c  o  m
            node.setNodeValue("42");
        }
    }
}

From source file:Main.java

public static final void setNodeValue(Node node, String value) {
    if (value == null)
        value = "";
    NodeList childNodes = node.getChildNodes();
    int len = childNodes == null ? 0 : childNodes.getLength();
    for (int i = 0; i < len; i++) {
        Node n = childNodes.item(i);
        n.setNodeValue(value);
    }//from   w w w. j a  v  a2  s .  c  om
}

From source file:Main.java

/**
 * Adds the attribute to the given node/*  www. j  av a 2 s.  c  om*/
 * @param doc
 * @param node
 * @param attrName
 * @param attrValue
 */
public static void addAttr(Document doc, Node node, String attrName, Object attrValue) {
    Node attr = doc.createAttribute(attrName);
    attr.setNodeValue(attrValue.toString());
    node.getAttributes().setNamedItem(attr);
}

From source file:Main.java

public static void setAttributeValue(Node node, String attName, String attValue) {
    Node attr = node.getAttributes().getNamedItem(attName);
    attr.setNodeValue(attValue);
}

From source file:Main.java

public static void addAttribute(Document doc, String name, Element e, String value) {
    Node attrNode = doc.createAttribute(name);
    attrNode.setNodeValue(value);
    NamedNodeMap attrs = e.getAttributes();
    attrs.setNamedItem(attrNode);/*from  ww w  . j  a  v  a2s  . co  m*/
}

From source file:Main.java

public static void setAttribute(Node node, String key, String value) {
    Node attributeNode = node.getOwnerDocument().createAttribute(key);
    attributeNode.setNodeValue(value);

    node.getAttributes().setNamedItem(attributeNode);
}

From source file:Main.java

public static Node createAttributeNode(Document document, String str, String str2) {
    Node createAttribute = document.createAttribute(str);
    if (createAttribute != null) {
        createAttribute.setNodeValue(str2);
    }/*from   ww  w.  j  a v a 2 s  .c om*/
    return createAttribute;
}

From source file:Main.java

/**
 * Set the value of element/* w ww  . ja va  2  s  .  c  o m*/
 *
 * @param element
 * @param val
 */
public static void setElementValue(Element element, String val) {
    Node node = element.getOwnerDocument().createTextNode(val);
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nd = nl.item(i);
        if (nd.getNodeType() == Node.TEXT_NODE) {
            nd.setNodeValue(val);
            return;
        }
    }
    element.appendChild(node);
}

From source file:Main.java

private static void cleanWhiteList(Node node, ArrayList<String> whiteList) {
    if (whiteList.contains(node.getLocalName())) {
        node.setNodeValue(null);
        node.setTextContent(null);/*from  w  ww . j av a 2  s .co m*/
        // System.err.println("haha");
    }
    NodeList children = node.getChildNodes();
    if (children.getLength() != 0) {
        for (int i = 0; i < children.getLength(); i++)
            cleanWhiteList(children.item(i), whiteList);
    }

}

From source file:Main.java

public static boolean setAttributeValue(final Element target, final String attributeName, final String value) {
    final NamedNodeMap map = target.getAttributes();
    final Node attributeValueHolder = map.getNamedItem(attributeName);
    if (attributeValueHolder != null) {
        attributeValueHolder.setNodeValue(value);
        return true;
    }//from   ww  w .j  a v  a 2s.c  o m
    return false;
}