Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Simpe XML question. You can make yourself look smart!

843834Oct 21 2003 — edited Oct 21 2003
I have the following xml file:
<?xml version="1.0"?>
<ToolConfig>
	<database_info></database_info>
	<spelling_info></spelling_info>
	<server_info></server_info>
	<concept_list></concept_list>
</ToolConfig>
I have the following java program:
import java.io.*;
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.SAXException;

public class ConfigParams
{
    public ConfigParams()
    {
        String configFile = new String("C:\\tomcat\\webapps\\QueryTool\\WEB-INF\\ToolConfig.xml");
        try
        {
            DOMParser parser = new DOMParser();
            parser.parse(configFile);
            org.w3c.dom.Document configDoc = parser.getDocument();
            NodeList nodeList = configDoc.getChildNodes();
            for (int j=0; j<nodeList.getLength(); j++)
            {
                System.err.println(j+": "+nodeList.item(j).getLocalName());
            }
            System.err.println("-----");
            Node topNode = nodeList.item(0);
            NodeList sectionNodes = topNode.getChildNodes();
            for (int j=0; j<sectionNodes.getLength(); j++)
            {
                System.err.println(j+": "+sectionNodes.item(j).getLocalName());
            }
            System.err.println("-----");
        }
        catch (SAXException se)
        {
            System.err.println("SAX EXCEPTION");
            se.printStackTrace();
        }
        catch (IOException ioe)
        {
            System.err.println("IO EXCEPTION");
            ioe.printStackTrace();
        }
    }
    

    public static void main(String[] args)
    {
        ConfigParams cp = new ConfigParams();
    }
}
The program outputs the following:
0: ToolConfig
-----
0: null
1: database_info
2: null
3: spelling_info
4: null
5: server_info
6: null
7: concept_list
8: null
-----
My question is: Why does it find nine child nodes when I only have four child nodes in the XML file? Why is every other child node null?

Thanks,

Jon
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 18 2003
Added on Oct 21 2003
3 comments
86 views