Results 1 to 3 of 3

Thread: XML DOM Parser Problem

  1. #1
    Join Date
    Jun 2006

    XML DOM Parser Problem

    I have some problem when try to parse an XML file.

    Here is my code.
    package app.test;
    import java.io.*;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    import org.xml.sax.*;
    public class BasicDom {
        public static void main(String[] args) {
            Document doc = parseXmlFile("test.xml", false);
            System.out.println("Document = "+doc);
        // Parses an XML file and returns a DOM document.
        // If validating is true, the contents is validated against the DTD
        // specified in the file.
        public static Document parseXmlFile(String filename, boolean validating) {
            try {
                // Create a builder factory
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                // Create the builder and parse the file
                Document doc = factory.newDocumentBuilder().parse(new File(filename));
                return doc;
            } catch (SAXException e) {
                // A parsing error occurred; the xml input is not valid
            } catch (ParserConfigurationException e) {
            } catch (IOException e) {
            return null;
    When compile and run with jdk 1.4 there is no problem.

    Document = org.apache.crimson.tree.XmlDocument@150bd4d
    But when compile and run with jkd 1.5 it show null document.

    Document = [#document: null]
    Does anyone know what is the problem when try to use jdk1.5?

  2. #2
    Join Date
    Dec 2003
    Bucharest, ROMANIA
    I guess that this is rather a java problem, not an XML one. I'll move the thread in the Java Forum

  3. #3
    Join Date
    Jul 2006

    There is nothing wrong with your code, it works fine... The reason you're getting confused is that the XML parser changed from Crimson to Xerces between java 1.4 and 1.5. It turns out that the Xerces parser by default has the following 'toString' method:
    return "["+getNodeName()+": "+getNodeValue()+"]";
    The node value for the document is always null, hence the printout...

    Try instead:
    System.out.println("Document = " + doc.getDocumentElement().getNodeName());
    and you should see the name of the top level XML element.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center

Recent Articles