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.