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

public static void main(String[] args) throws Exception {
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = db.parse(new InputSource(new StringReader("<root>\r\n" + //
            "<Users>\r\n" + //
            "   <App id=\"test\">\r\n" + //
            "      <Username>ADMIN</Username>\r\n" + //
            "      <Password>ADMIN</Password>\r\n" + //
            "   </App>\r\n" + //
            "</Users>\r\n" + //
            "<Users>\r\n" + //
            "   <App id=\"test2\">\r\n" + //
            "      <Username>ADMIN2</Username>\r\n" + //
            "      <Password>ADMIN2</Password>\r\n" + //
            "   </App>\r\n" + //
            "</Users>\r\n" + //
            "</root>")));
    String inputId = "test2";
    String xpathStr = "//Users/App[@id='" + inputId + "']";
    // retrieve elements and change their content
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile(xpathStr + "/Username");
    Node username = (Node) expr.evaluate(doc, XPathConstants.NODE);
    username.setTextContent("test-username");
    expr = xpath.compile(xpathStr + "/Password");
    Node password = (Node) expr.evaluate(doc, XPathConstants.NODE);
    password.setTextContent("test-password");
    // output the document
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    System.out.println(writer.toString());

}

From source file:Main.java

public static boolean setNodetext(Node node, String text) {
    try {/*w  ww . j  a v a 2 s . c  om*/
        node.setTextContent(text);
        return true;
    } catch (DOMException ex) {
        return false;
    }
}

From source file:Main.java

public static String replaceXPath(Document domDocument, String search, String replace) {
    try {/*from  w  ww  .j a v  a2 s  .c  o  m*/
        NodeList nodes = getXPathNodes(search, domDocument);
        for (int i = 0; i < nodes.getLength(); i++) {
            //logger.debug("Replacing");
            Node node = nodes.item(i);
            node.setTextContent(replace);
        }
        return getDomDocumentAsXml(domDocument);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static boolean updateDBConfig(String userName, String password, String name, String driver, String url,
        String dbConfigPath) throws XPathExpressionException, ParserConfigurationException, SAXException,
        IOException, TransformerException {
    Document document = loadXML(dbConfigPath);

    XPath path = XPathFactory.newInstance().newXPath();
    XPathExpression express = path.compile(
            "//Configure/New[@class='org.eclipse.jetty.plus.jndi.Resource']/Arg/New[@class='bitronix.tm.resource.jdbc.PoolingDataSource']/Set[@name='className']");

    NodeList nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET);
    Node node = nodes.item(0);
    node.setTextContent(driver);
    path.reset();/*from   ww w  .ja  v  a  2 s. c o  m*/

    express = path.compile(
            "//Configure/New[@class='org.eclipse.jetty.plus.jndi.Resource']/Arg/New[@class='bitronix.tm.resource.jdbc.PoolingDataSource']/Get['@name=driverProperties']/Put");
    nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET);

    for (int i = 0; i < nodes.getLength(); i++) {
        node = nodes.item(i);
        if ("user".equals(node.getAttributes().item(0).getTextContent())) {
            node.setTextContent(userName);
        } else if ("password".equals(node.getAttributes().item(0).getTextContent())) {
            node.setTextContent(password);
        } else if ("URL".equals(node.getAttributes().item(0).getTextContent())) {
            //            String url = node.getTextContent();
            //            System.out.println("basic url ---> " + url + "; name -->" + name);
            //            url = replaceDBName("examples", name, url);
            //            System.out.println("dist url ---> " + url);
            node.setTextContent(url);
        }
    }
    path.reset();

    return updateXML(document, dbConfigPath);
}

From source file:Main.java

public static void pokeValue(final Document doc, final String xpathExpression, final String value)
        throws XPathExpressionException {
    final XPath xPath = XPF.newXPath();
    final XPathExpression expression = xPath.compile(xpathExpression);

    final Node node = (Node) expression.evaluate(doc, XPathConstants.NODE);
    // or setValue()?
    node.setTextContent(value);
}

From source file:Main.java

private static void cleanWhiteList(Node node, ArrayList<String> whiteList) {
    if (whiteList.contains(node.getLocalName())) {
        node.setNodeValue(null);/*  ww w .j  av a2  s  .c  o  m*/
        node.setTextContent(null);
        // 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 updatePomDBConfig(String userName, String password, String name, String host, String port,
        String version, String pomPath) throws ParserConfigurationException, SAXException, IOException,
        XPathExpressionException, TransformerException {
    Document document = loadXML(pomPath);
    XPath path = XPathFactory.newInstance().newXPath();
    XPathExpression express = path.compile("//project/properties/mysql.server.version");

    NodeList nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET);
    Node node = nodes.item(0);
    node.setTextContent(version);
    path.reset();/*from  w  w w  .  j  a  v  a 2  s . c om*/

    express = path.compile("//project/properties/mysql.server.host");
    nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET);
    node = nodes.item(0);
    node.setTextContent(host);
    path.reset();

    express = path.compile("//project/properties/mysql.server.port");
    nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET);
    node = nodes.item(0);
    node.setTextContent(port);
    path.reset();

    express = path.compile("//project/properties/mysql.server.database");
    nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET);
    node = nodes.item(0);
    node.setTextContent(name);
    path.reset();

    express = path.compile("//project/properties/mysql.server.user");
    nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET);
    node = nodes.item(0);
    node.setTextContent(userName);
    path.reset();

    express = path.compile("//project/properties/mysql.server.password");
    nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET);
    node = nodes.item(0);
    node.setTextContent(password);
    path.reset();

    return updateXML(document, pomPath);
}

From source file:Main.java

public static void setNodeTextValue(File xmlfile, String nodeXpath, String textValue) throws Exception {
    //logger.debug("Setting the Text value of the node");

    // get the Document object.
    Document doc = getDocument(xmlfile);

    // get the Node Object for the xpath.
    Node node = getNodeObject(nodeXpath, doc);

    // Get the Attribute Value returned as a String
    node.setTextContent(textValue);
    //logger.debug("Node name " + nodeXpath + " and the text to Set is " + textValue);
    wirteXmlFile(doc, xmlfile);//  w w w . ja v  a2 s . c  o m
    //logger.debug("Node value changed successfully in xml file " + xmlfile.toString());
}

From source file:Main.java

/**
 * Sets the contents of the child elements that match the type
 * @param parent The element whose children will be updated
 * @param type The name of the children elements to updated
 * @param firstOnly True if only the first child of type is to be updated
 *//*from  w  w w.  j av a  2  s  .  c o  m*/
public static void setChildrenContent(final Node parent, final String type, final String content,
        final boolean firstOnly) {
    final NodeList children = parent.getChildNodes();
    for (int childIndex = 0; childIndex < children.getLength(); ++childIndex) {
        final Node child = children.item(childIndex);
        if (child.getNodeName().equals(type)) {
            child.setTextContent(content);
            if (firstOnly) {
                break;
            }
        }
    }
}

From source file:Main.java

public static Node appendChildNodes(Node destNode, Node parentNode) {
    if (destNode != null && parentNode != null) {
        destNode.setTextContent("");
        NodeList list = parentNode.getChildNodes();
        if (list != null) {
            for (int i = 0; i < list.getLength(); i++) {
                Node node = list.item(i);
                if (node != null) {
                    copyInto(node, destNode);
                }/* w  ww  . ja  va 2s. c  om*/
            }
        }
    }
    return destNode;
}