Ok, so I have this XML file that I'm reading:
<?xml version="1.0"?>
<APOLLERSETTINGS>
<SYSTEM>
<VERSION>3.1</VERSION>
<SMTPSRV>blue</SMTPSRV>
<SMTPPORT>29</SMTPPORT>
<SMTPUSR>Me</SMTPUSR>
<SMTPPSW>Password</SMTPPSW>
<EMAILFROM>lodewyk.duminy@gmail.com</EMAILFROM>
<EMAILTO>lodewyk.duminy@gmail.com</EMAILTO>
<ALTEMAILTO>lodewyk.duminy@gmail.com</ALTEMAILTO>
</SYSTEM>
<JOB>
<JOBNAME>The Name</JOBNAME>
<INPUTFOLDER>C:\</INPUTFOLDER>
<FILEMASK>*.txt</FILEMASK>
<PROCESSORDER></PROCESSORDER>
<RETAINFOLDER>C:\</RETAINFOLDER>
<OVERRIDEFOLDER>C:\</OVERRIDEFOLDER>
<ERRORFOLDER>C:\</ERRORFOLDER>
<POLLINTERVAL>6000</POLLINTERVAL>
<JOBBATCHFILEPATH>C:\</JOBBATCHFILEPATH>
<JOBBATCHPARAMS>three</JOBBATCHPARAMS>
<JOBLOGPATH>C:\</JOBLOGPATH>
<JOBEMAILTO>lodewyk.duminy@gmail.com</JOBEMAILTO>
<JOBALTEMAILTO>lodewyk.duminy@gmail.com</JOBALTEMAILTO>
</JOB>
</APOLLERSETTINGS>
This is the code that is reading the file:
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, "3.1", "red");
}
public void changeContent(Document doc, String newname, String newemail) {
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.getNodeName().equals("#text")) {
System.out.println(node.getNodeName() + ":" + node.getNodeValue());
//}
//System.out.println(node.getNodeName() + ":" + node.getNodeValue());
}
}
}
}
}
What I'm trying to do is to print the node names and values for all the <SYSTEM>'s child nodes.
Getting the names works. But the values are all returned as nulls. On top of that, some other stuff is being printed as well. I don't know where it's coming from.
Here is the output:
run:
#text:
VERSION:null
#text:
SMTPSRV:null
#text:
SMTPPORT:null
#text:
SMTPUSR:null
#text:
SMTPPSW:null
#text:
EMAILFROM:null
#text:
EMAILTO:null
#text:
ALTEMAILTO:null
#text:
BUILD SUCCESSFUL (total time: 0 seconds)
Can anyone explain to me please why all the node values are being returned as null?
Also, why is "#text:" being printed out the whole time?