[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: DTD parser in c#


c string parser
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.