aspose file tools*
The moose likes XML and Related Technologies and the fly likes Why is setValidate not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Why is setValidate not working" Watch "Why is setValidate not working" New topic
Author

Why is setValidate not working

Rob Bass
Ranch Hand

Joined: Aug 28, 2001
Posts: 67
Maybe I am doing something totally wrong, here is my XML:
<?xml version="1.0"?>
<!DOCTYPE book SYSTEM "bookgram.dtd">
<book>
<title>Professional Java Programming</title>
<author>Brett Spell</author>
<publisher>Wrox Press</publisher>
<tableOfContents showPageNumbers="Yes">
<tocEntry>Printing</tocEntry>
<tocEntry>Cut and Paste</tocEntry>
<tocEntry>Drag and Drop</tocEntry>
</tableOfContents>
</book>
and dtd:
<!ELEMENT book (title, author, publisher, tableOfContents)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT publisher (#PCDATA)>
<!ELEMENT tableOfContents (tocEntry+)>
<!ATTLIST tableOfContents
showPageNumbers CDATA "yes"
>
<!ELEMENT tocEntry (#PCDATA)>

My java code:
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
this.fileNameNPath = fileToOpen;
factory.setValidating(true);
factory.setNamespaceAware(true);
this.doc = builder.parse(fileToOpen);
return true;
I edited the XML like this:
<?xml version="1.0"?>
<!DOCTYPE book SYSTEM "bookgram.dtd">
<book>
<title>Professional Java Programming</title>
<author>Brett Spell</author>
<addsomecrap/>
<publisher>Wrox Press</publisher>
<tableOfContents showPageNumbers="Yes">
<tocEntry>Printing</tocEntry>
<tocEntry>Cut and Paste</tocEntry>
<tocEntry>Drag and Drop</tocEntry>
</tableOfContents>
</book>
I then thought when I ran my Java code it would bomb but it still works,
the XML no longer confirms to the DTD, why is it still able to parse
without error???
HELP
Rob
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Rob,
When you change the settings of the document builder factory, it does not affect the previously created document builders. All you need to do is move the line instanciating a new document builder after the ones that changes the settings of the factory, like this:

Hope this helps.
Best regards,
Beno�t
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is setValidate not working