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 "Grengbondai, Jules C." <jg...@cincom.com> on 2000/07/17 15:09:39 UTC

Cannot obtain elements from the DTD(documentTypeImpl Object)

Dear Sir or Madam:

I'm trying to process the DocumentTypeImpl instance. I'm interested in the
element definitions in the dtd.

The attached java program returns an empty NamedNodeMapImpl object.
Am I invoking the method correctly?

Please understand that I'm learning XML and I may not properly call the
methods.


	Version number of Xerces-J = 1.1.2 
  	Version number of JDK  = 1.2.2
	Sample XML file = department.xml
            Sample DTD file = department.dtd
            Sample Java Program = JulesTest.java

-------------------------------------Source code
-----------------------------------------------------------------
import java.io.File;
import org.xml.sax.InputSource;

import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.dom.*;
import org.w3c.dom.Document;

public class JulesTest
{

   public static void main(String[] argv)
   {
      try
      {
         if (argv.length == 1) {

           File file = new File(argv[0]);

            InputSource src = new InputSource(getFileURL(file));

            DOMParser parser = new DOMParser();
            //parser.setFeature("http://xml.org/sax/features/validation",
true);
            parser.parse(src);
            // get the document
            Document doc = parser.getDocument();
            // get docType
            DocumentTypeImpl docType = (DocumentTypeImpl)doc.getDoctype();

            System.out.println("docType Name " +
doc.getDoctype().getName());

            NamedNodeMapImpl elementDefMap =
(NamedNodeMapImpl)docType.getElements();
            int elementdefCount = elementDefMap.getLength();

		// Note that the next line always output 0
            //System.out.println("docType ElementDefinition count is " +
docType.getElements().getLength());
            System.out.println("docType ElementDefinition count is " +
elementdefCount);


          }
          else
	    {
              System.out.println("Usage: java JulesTest
<input-xml-file>\n");
              System.out.println("for example: java JulesTest
department.xml\n");
              return ;

           }
       }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }

   static String getFileURL(File file)
   {
      return "file:///" + file.getAbsolutePath();
   }


}

-------------------------------------DTD------------------------------------
----------------------------------------------

<!ELEMENT department (employee)*>
<!ELEMENT employee (name, (email | url))>
<!ATTLIST employee id CDATA #REQUIRED>
<!ELEMENT name (#PCDATA)>
<!ELEMENT email (#PCDATA)>
<!ELEMENT url EMPTY>
<!ATTLIST url href CDATA #REQUIRED>

-------------------------------------------XML
file-----------------------------------------------------------

<?xml version="1.0"?>
<!DOCTYPE department SYSTEM "department.dtd">
<department>
  <employee id="JCG">
    <name>Jules Grengbondai</name>
    <email>jgrengbondai@cincom.com</email>
  </employee>

  <employee id="CAG">
    <name>Crephat Grengbondai</name>
    <email>cgrengbondai@creance.com</email>
  </employee>

  <employee id="LJG">
    <name>Lance Grengbondai</name>
    <url href="http://www.creance.com/~lance/"/>
  </employee>
</department>


Regards,
Jules