You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by th...@telenor.com on 2000/10/12 17:03:03 UTC

Document.getDoctype() return null

Hi

I sent a problem description earlier on that I want to substantiate a little
more. Basically the problem is that I can't get the document type from a
parsed document. I receive a null object when using Document.getDoctype(). 

I use jdk1.1.8 and xerces1.2.0/xerces1.1.1.

The appended example (with source, xml and dtd; tried to make it as short as
possible) shows my problem. I hope anyone can notice something I should or
should not do, or have a fix/work-around for a bug.

**** Test.java
import java.io.*;
import org.w3c.dom.*;
import org.apache.xerces.dom.*;
import org.apache.xerces.parsers.*;
import org.xml.sax.*;

public class Test
{
    public static class MyDoc extends DocumentImpl
    {
        public Element createElement(String tagName) throws DOMException
        {
            if ("hi".equals(tagName))
                return new MyElem(this, tagName);
            else
                return super.createElement(tagName);
        }
    }
    public static class MyElem extends ElementImpl
    {
        public MyElem(DocumentImpl doc, String tag)
        {
            super(doc, tag);
        }
    }
    public static class MyParser extends DOMParser
    {
        public MyParser()
        {
            super();
            try
            {
                setDocumentClassName(MyDoc.class.getName());
            }
            catch (Exception exc)
            {
                exc.printStackTrace();
            }
        }
    }
    public static void main(String[] args)
    {
        try
        {
            MyParser parser = new MyParser();
            parser.parse(new InputSource(new FileReader(new
File(args[0]))));
            MyDoc doc = (MyDoc) parser.getDocument();
            MyElem elem = (MyElem) doc.getElementById("hi");
            if (doc.getDoctype() != null)
            {
                System.out.println("System id = " +
doc.getDoctype().getSystemId());
            }
            else
            {
                System.out.println("Document type null");
            }
        }
        catch (Exception exc)
        {
            exc.printStackTrace();
        }
    }
}
****

**** my.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hi SYSTEM "my.dtd" >
<hi/>
****

**** my.dtd
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT hi EMPTY>
****

have a nice day
ThorĂ…ge
--
Telenor Mobil, Tlf: 22 78 47 59, Mob: 95 94 75 64,
thor-age.eldby@telenor.com
"All suspects are guilty! Otherwise they wouldn't be suspects; would they?"