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?

Recommended Answers

All 3 Replies

First of all here is a small misconception:
This:

<VERSION>3.1</VERSION>
<SMTPSRV>blue</SMTPSRV>

is not Two nodes but Three!

Node 1:
<VERSION>3.1</VERSION>

Node 3:
<SMTPSRV>blue</SMTPSRV>

Node 2:

<VERSION>3.1</VERSION>
<SMTPSRV>blue</SMTPSRV>

The new line character between </VERSION> and <SMTPSRV>

So when you print this: System.out.println(node.getNodeName() + ":" + node.getNodeValue()); For Node <VERSION>3.1</VERSION> :
You have Name = VERSION,
Value is null, because <VERSION> doesn't have a value

And for the new line:
Name: Doesn't have a name because it is just text in the file so you get #text
Value: you get the new line character, which is why you get that empty line.

Try printing this:

System.out.println(node.getNodeName() +":"+([B]node.getNodeType()[/B]==Node.TEXT_NODE)
+":"+[B]node.getNodeType()[/B]);

You will see the it is true only for the #text, and for the nodes whose values you want is >1<

So you can do this to avoid the free text that is between the nodes in your file:

<VERSION>3.1</VERSION> [B]<new line>[/B]
[B]<some spaces>[/B]<SMTPSRV>blue</SMTPSRV>
       <SMTPPORT>29</SMTPPORT>
if (node.getNodeType()==Node.ELEMENT_NODE) {
   System.out.println(node.getNodeName() +":" + node.[B]getTextContent()[/B]);
}

Also a good way to find out what type of node you have is this:

public static void main(String[] args) {
        System.out.println("ATTRIBUTE_NODE             :"+Node.ATTRIBUTE_NODE);
        System.out.println("CDATA_SECTION_NODE         :"+Node.CDATA_SECTION_NODE);
        System.out.println("COMMENT_NODE               :"+Node.COMMENT_NODE);
        System.out.println("DOCUMENT_FRAGMENT_NODE     :"+Node.DOCUMENT_FRAGMENT_NODE);
        System.out.println("DOCUMENT_NODE              :"+Node.DOCUMENT_NODE);
        System.out.println("DOCUMENT_TYPE_NODE         :"+Node.DOCUMENT_TYPE_NODE);
        System.out.println("ELEMENT_NODE               :"+Node.ELEMENT_NODE);
        System.out.println("ENTITY_NODE                :"+Node.ENTITY_NODE);
        System.out.println("ENTITY_REFERENCE_NODE      :"+Node.ENTITY_REFERENCE_NODE);
        System.out.println("NOTATION_NODE              :"+Node.NOTATION_NODE);
        System.out.println("PROCESSING_INSTRUCTION_NODE:"+Node.PROCESSING_INSTRUCTION_NODE);
        System.out.println("TEXT_NODE                  :"+Node.TEXT_NODE);
        /*
        System.out.println("DOCUMENT_POSITION_CONTAINED_BY:"+Node.DOCUMENT_POSITION_CONTAINED_BY);
        System.out.println("DOCUMENT_POSITION_CONTAINS:"+Node.DOCUMENT_POSITION_CONTAINS);
        System.out.println("DOCUMENT_POSITION_DISCONNECTED:"+Node.DOCUMENT_POSITION_DISCONNECTED);
        System.out.println("DOCUMENT_POSITION_FOLLOWING:"+Node.DOCUMENT_POSITION_FOLLOWING);
        System.out.println("DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC:"+Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC);
        System.out.println("DOCUMENT_POSITION_PRECEDING:"+Node.DOCUMENT_POSITION_PRECEDING);
        */
    }

WOW. Just blew me away... it works 100% now!

Thank you soooo much!

That has been keeping me up for about 3 hours. Appreciate it! Have a nice day!

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.