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();
System.out.println(jobElem.getNodeName());

if (jobElem.getChild("JOBNAME").getValue().equals("Second job")) {
jobElem.getChild("INPUTFOLDER").setText("MY-NEW-LOCATION");
}

}
}
}
 */

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();
        factory.setValidating(validating);

        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("***");
                        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.

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);
myFileIO.close();

Hope this was helpful

commented: Good work +5

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.