OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Re: [xml-dev] DTD parser in c#

[ Lists Home | Date Index | Thread Index ]

Hi Michelle,

>If you are just interested in parsing a DTD, you need a dummy wrapper document
>so that a SAX parser can process the DTD, as Jeff already pointed out.
>
>Karl
>
>  
>
DTD handling is part of the "interfaces to SAX2 facilities that 
conformant SAX drivers won't necessarily support." (from the javadoc). 
Serious implementations seem to support it though.

Here is the essential code (it is not complete, please don't try to 
compile it). I use Scala (http://scala.epfl.ch), but since it looks 
close enough to Java and C# you don't have to be afraid : )
---
import org.xml.sax.InputSource;
import java.io.StringReader;
...
  private def inputsrc( sysID:String ):InputSource = {
    // create isrc for a fake doc referencing the DTD
    val  isrc:InputSource = new InputSource(
      new StringReader("<!DOCTYPE doc SYSTEM \""+ sysID +"\"><doc></doc>")
    );
    val curDir:String = System.getProperty("user.dir");
    isrc.setSystemId( "file:///"+curDir+"/fubar.xml" );
    isrc;
  }
...
---
The dummy system identifier was only needed for the crimson parser that 
comes with the JDK, as it complained otherwise. Try to leave it out.

I assume you know how to instantiate a SAX parser and register a handler 
with it. Then try this

---
val DECL_HANDLER = "http://xml.org/sax/properties/declaration-handler";;
try   { parser.setProperty( DECL_HANDLER, myHandler ); }
catch { case e:SAXException => e.printStackTrace(System.err); }
---  

Last, here is what 'myHandler' has to implement:
---
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.ext.DeclHandler;

/** SAX handler, reacts to events during declaration parsing */
class MainHandler extends DefaultHandler with DeclHandler {
   
  /** encountered element declaration */
  def elementDecl( name:String, contentModel:String ) = {...do something 
with elementDecl...}

  /** encountered attribute declaration. */
  def attributeDecl(elementName:String,
                    attributeName:String,
                    tpe:String,
                    valueDefault:String,
                    value:String ) = {...do something with attrib decl... }
                     
  /** Internal entity declaration. */
  def internalEntityDecl( name:String, text:String ) = {/*...etc...*/}
     
  /** External entity declaration. */
  def externalEntityDecl(name:String,pubId:String,sysId:String)= 
{/*...etc...*/}
     
}
---

hope this helps,
cheers,
Burak




 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS