0

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.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by P00dle
Featured Replies
  • You must use the transformer to save the updated DOM tree back to the file. The sample code is, [CODE] 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(); [/CODE] … Read More

  • 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: [CODE] System.out.println(node.getNodeName() + ":" + node.[B][COLOR="Green"]getTextContent()[/COLOR][/B]); [/CODE] So why are using this: [U][I]node.setNodeValue("")[/I][/U] in order to change its value? Use the … Read More

1

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

Comments
Good work
1

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.

0

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.