Hi Ralf,

Thanks so much for your response.  

I tried you mentioned, but it did not work.

DocumentTypeImpl dtd = (DocumentTypeImpl)((DocumentImpl)doc).getDoctype();
NamedNodeMap map = dtd.getElements();

and when I do this:

System.out.println("length="+map.getLength());

It prints:
length=0

I am sure it recognizing my doctype because when I call:

System.out.println("name="+dtd.getName());

It does print the name of the doctype.

Any idea why I am getting length=0, there is 88 elements defined in my 
doctype.  I am using the latest version of xerces (version 1.02)

Thanks,
Chris

------------------------- snip -------------------------------

import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.dom.DocumentImpl;
import org.apache.xerces.dom.DocumentTypeImpl;

import org.xml.sax.Parser;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;

import org.w3c.dom.DocumentType;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Document;

public class XMLTester implements ErrorHandler {

    /**
     * Constructor.
     */
    public XMLTester () {
    }

    public static void main(String args[]) throws Exception {

        DOMParser parser  = new DOMParser();
        parser.setFeature("http://xml.org/sax/features/validation";, true);
        parser.setErrorHandler(this);
        parser.parse("test.xml");

        Document doc = parser.getDocument();
        
        DocumentTypeImpl dtd = 
(DocumentTypeImpl)((DocumentImpl)doc).getDoctype();
        NamedNodeMap map = dtd.getElements();

        System.out.println("name="+dtd.getName());
        System.out.println("length="+map.getLength());
        for (int i=0; i<map.getLength(); i++) {
            System.out.println(map.item(i));
        }

        public void error(SAXParseException exception) {
            System.err.println("ERROR: "+exception);
        }
        public void fatalError(SAXParseException exception) {
            System.err.println("FATAL ERROR: "+exception);
        }
        public void warning(SAXParseException exception) {
            System.err.println("WARNING: "+exception);
        }
    }
    
------------------------- snip -------------------------------    
    












"Ralf I. Pfeiffer" wrote:
> 
> With the current DOMParser you can:
> 
> DocumentTypeImpl dtd = ((DocumentImpl)doc).getDoctype();
> NamedNodeMap map = dtd.getElements();
> 
> Chris Gokey wrote:
> 
> > Hi everyone,
> >
> > I'm hoping that someone could point me in the right direction regarding
> > what
> > classes I need in order to access element definitions in a DTD.
> >
> > Consider the following DTD:
> >
> > <!ELEMENT Personnel (Role+, First_Name?, Middle_Name?, Last_Name?,
> >   Email*, Phone*, FAX*, Address?)>
> >
> > <!ELEMENT Role (#PCDATA)>
> > <!ELEMENT First_Name (#PCDATA)>
> > <!ELEMENT Middle_Name (#PCDATA)>
> > <!ELEMENT Last_Name (#PCDATA)>
> > <!ELEMENT Email (#PCDATA)>
> > <!ELEMENT Phone (#PCDATA)>
> > <!ELEMENT FAX (#PCDATA)>
> > <!ELEMENT Address (#PCDATA)>
> >
> > I'd like an API something like this:
> >
> > // some class that would parse a DTD and create an intermediate
> > representation
> > for
> > // accessing its elements (like a DOM).
> > DTDParser parser = new DTDParser();
> > parser.parse("dif.dtd");
> >
> > ElementList list = parser.getElements();
> > int len = list.getLength();
> > for (int i=0; i<len; i++) {
> >   String elementName = list.item(i).getElementName();
> >   String definition = list.item(i).getDefintion();
> >   System.out.println("elementName="+elementName);
> >   System.out.println("definition="+defintion);
> >   System.out.println("\n");
> > }
> >
> > In which the code above might produce the following output:
> >
> > elementName = Personnel
> > definition = (Role+, First_Name?, Middle_Name?, Last_Name?,
> >   Email*, Phone*, FAX*, Address?)
> >
> > elementName = Role
> > definition = (#PCDATA)
> >
> > elementName = FirstName
> > defintion = (#PCDATA)
> >
> >        .
> >        .
> >        .
> >
> > I would imagaine the Xerces library must have something to help me achieve
> > this functionality?
> >
> > Thanks in advanced...
> >
> > Chris
> >
> > --
> > Christopher D. Gokey, Raytheon ITSS, NASA/GCMD
> > 18 Martin Road, Shelburne Falls, MA  01370
> > Phone: Voice (413) 625-8129 / FAX 208-248-9055
> > [EMAIL PROTECTED] / http://gcmd.nasa.gov
> 
> --
> <person name="Ralf I. Pfeiffer"
>  loc="IBM JTC, Cupertino, CA"
>  email1="[EMAIL PROTECTED]"
>  email2="[EMAIL PROTECTED]"
> />
> 
> 

-- 
Christopher D. Gokey, Raytheon ITSS, NASA/GCMD
18 Martin Road, Shelburne Falls, MA  01370
Phone: Voice (413) 625-8129 / FAX 208-248-9055
[EMAIL PROTECTED] / http://gcmd.nasa.gov

Reply via email to