The code I'm using to change XML content isn't working...
Does anyone have any idea what I'm doig wrong:

package domtest;
public class Main {

public static void main(String args[]) throws Exception {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
DOMBuilder domBuilder = new DOMBuilder();
Document jdomDoc = domBuilder.build(builder.parse(new File("src/home/projects/misc/Test.xml")));
Iterator iter = jdomDoc.getDocumentElement().getChildNodes().iterator();
while (iter.hasNext()) {
Element jobElem = (Element) iter.next();

if (jobElem.getChild("JOBNAME").getValue().equals("Second job")) {


import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class Main {

    public static void main(String[] args) throws Exception {
        boolean validating = false;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        Document doc = factory.newDocumentBuilder().parse(new File("C:\\Documents and Settings\\Administrator.APC-CT2\\Desktop\\lodewyk.xml"));
        new Main().changeContent(doc);

    public void changeContent(Document doc) {

        Element root = doc.getDocumentElement();

        NodeList list = doc.getElementsByTagName("*");
        //System.out.println("XML Elements: ");
        for (int i = 0; i < list.getLength(); i++) {
            // Get element
            Element element = (Element) list.item(i);
            //System.out.println(element.getNodeName() + " : " + element.getTextContent());
            if (element.getNodeName().equals("SYSTEM")) {
                NodeList list2 = element.getChildNodes();
                for (int x = 0; x < list2.getLength(); x++) {
                    Node node = (Node) list2.item(x);

                    if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("SMTPUSR")) {
                        System.out.println(node.getNodeName() + ":" + node.getTextContent());
                        node.setNodeValue("Someone Else");
                        System.out.println(node.getNodeName() + ":" + node.getTextContent());



The problem that I don't understand:

node.setNodeValue("Someone Else");

Doesn't actually change the node's content, since the printout before and after are exactly the same, and the file also remains unchanged.

7 Years
Discussion Span
Last Post by P00dle

You must use the transformer to save the updated DOM tree back to the file.

The sample code is,

FileOutputStream myFileIO = new FileOutputStream(new File("C:\\Documents and Settings\\Administrator.APC-CT2\\Desktop\\lodewyk.xml"));
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(myFileIO);
transformer.transform(source, result);

Hope this was helpful

Votes + Comments
Good work

Also you are making the same mistake you did in your initial post.
Remember, in order to print the text value of the node you are using this method:

System.out.println(node.getNodeName() + ":" + node.[B]getTextContent()[/B]);

So why are using this: node.setNodeValue("") in order to change its value?

Use the equivalent setTextContent("") method.


Thanks :)
I changed that, and my whole program works now.

I've hit another snag, though, but I will start a new thread for it.

Appreciate the help!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.