Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SAX parser and DTD validation

Status
Not open for further replies.

Vepo

Programmer
Apr 25, 2001
75
SE
I'm trying to parse an XML-document and validate it against DTD.
If I got it right, I have to do it like this:
....
InputSourse is = new InputSource(...);
is.setSystemId("location of the dtd");
xmlParser.parse(is);

But despite of the form of the DTD-file I get following error:
org.xml.sax.SAXParseException: External parameter entity "%[dtd];" has characters after markup.

I tried to find out what that error means, but only found one page: and that didn't offer much help :(

I would be pleased of any help...
 
To use SAX, you must implement it's interfaces. At the top of your .cls module, put this:
[tt]
Implements IVBSAXContentHandler
Implements IVBSAXErrorHandler
[/tt]
And then you implement the functions that you need. Typically this will only be:
[tt]
IVBSAXContentHandler_startDocument
IVBSAXContentHandler_startElement
IVBSAXContentHandler_characters
IVBSAXContentHandler_endElement
IVBSAXErrorHandler_error
IVBSAXErrorHandler_fatalError
IVBSAXErrorHandler_ignorableWarning
[/tt]

To get the SAX parser going, you create a method in your class, and add code like:
[tt]
Dim objSAX As SAXXMLReader30
Set objSAX = New SAXXMLReader30
Set objSAX.contentHandler = Me 'This class handles content
Set objSAX.errorHandler = Me 'This class also handles errors

Call objSAX.parseURL("file:///" & ImportFilename)
[/tt]

There are a few problems with Microsoft's SAX implementation -- It tends to "fatalerror" a lot on accented characters. Also, any strange characters will also cause it to raise errors. Some of these can be gotten around by specifying the ISO-8859-1 character set encoding in your source XML file, but not all.

In order to validate this against a DTD, you will need to put code in the EndElement event to something like:
[tt]
Set objDom = New MSXML2.DOMDocument30

' Prepend the DTD to the node
If objDom.loadXML(m_sDTDDefinition & m_sCurrentNode) Then

[tab]' Validate the node against the DTD
[tab]Set objParseError = objDom.Validate
[tab]If objParseError.errorCode = 0 Then
[tab][tab]' Passed validation
[/tt]

Hope this helps.
Chip H.
 
Thanks Chip,
but it was my fault that I didn't mention that I'm using java.
So let me clarify the situation, infact the parser if JAXP1.1 which includes org.w3c.dom and org.xml.sax packages.

I can set the validation on by using SAXParserFactory's method setValidating(true);

The problem occurs when I try to define external DTD with
setSystemId(location of dtd) -method.
Because if your xml-file uses dtd and you are using validation, that must be done...

Any ideas?

But thanks anyway for your trouble, Chip
 
Ooops. Sorry, I didn't notice that you were using Java.

Unfortunately I'm not that good in Java (I can handle the basics), and haven't done XML in Java. But as a suggestion, what happens if you reverse the order of the calls -- call setSystemId() before setValidating(true)?

Chip H.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top