Ok, so I have this XML file that I'm reading:

<?xml version="1.0"?>

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();

if (jobElem.getChild("JOBNAME").getValue().equals("Second job")) {


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();

        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:


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?

First of all here is a small misconception:


is not Two nodes but Three!

Node 1:

Node 3:

Node 2:


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)

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>
if (node.getNodeType()==Node.ELEMENT_NODE) {
   System.out.println(node.getNodeName() +":" + node.[B]getTextContent()[/B]);

Edited 6 Years Ago by javaAddict: n/a

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("TEXT_NODE                  :"+Node.TEXT_NODE);

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!

This question has already been answered. Start a new discussion instead.