You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xerces.apache.org by Li Zhang <el...@yahoo.com> on 2000/02/07 07:34:29 UTC

validation feature for DOM parser

I have downloaded the binary release of Xerces 1.0.1 for Java and tried to
use its DOM parser to parse a xml file (with a dtd file for validation)
using the following code:

try {
  DOMParser parser = new DOMParser();
  parser.setFeature("http://xml.org/sax/features/validation",true);
  parser.parse(uri);
  doc = parser.getDocument();
}
catch(Exception e)
{
 ...
}

The problem is although there's an element ("Abbreviation" - see below xml
and dtd) in the xml file which is not defined in the dtd file, I haven't got
any exception. However when I use Microsoft XML Notepad I got an error
message about the undefined element.

The XML file is:

<?xml version="1.0"?>
<!DOCTYPE mystore SYSTEM "mystore.dtd">

<mystore>
<items>
  <book Name="xml spec" Code="9001">
   <Abbreviation>xml</Abbreviation>
   <Author>xml.org</Author>
  </book>
..
</items>
</mystore>

The dtd is defined as follows:

<!ELEMENT mystore items>
<!ELEMENT items (book)*>
<!ELEMENT book (Abbr?,Author*)>
<!ATTLIST book Name CDATA #REQUIRED Code CDATA #REQUIRED>
<!ELEMENT Abbr (#PCDATA)>
<!ELEMENT Author (#PCDATA)>

Is it a bug or am I missing something in the code? I'm using
JDK1.2.2/Windows NT4 + SP5/Xerces1.0.1 for Java.

Li


__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com

Re: validation feature for DOM parser

Posted by Andy Clark <an...@apache.org>.
Li Zhang wrote:
> The problem is although there's an element ("Abbreviation" - see 
> below xml and dtd) in the xml file which is not defined in the 
> dtd file, I haven't got any exception. However when I use 
> Microsoft XML Notepad I got an error message about the undefined 
> element.

In order to "see" non-fatal errors, you must register an error
handler. For example:

  DOMParser parser = new DOMParser();
  parser.setErrorHandler(new MyErrorHandler());

In addition, there is an error in your DTD:

> <!ELEMENT mystore items>

Should be:

<!ELEMENT mystore (items)>

-- 
Andy Clark * IBM, JTC - Silicon Valley * andyc@apache.org