• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Validating XML against DTD

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In our application we have a XML coming in. We want to validate the data coming in with respect to the DTD. (E.g maximum length for a particular tag of maximum integer value etc)
Can anyone help me with various alternatives to do so and information on performance for each approach
Thanks
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
If u are using jaxp.jar and DocumentParserFactory. I have a method of validating the document.
Just set the setValidating(true) for the DocumentParserFactory, once u apply parse() over the Parser object the document will be validated against the DTD and if u have any error a SAXParserException will be thrown.
Hope this helps u
cheers
Vels
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having a problemm doing what you suggested. I set validate to true and purposely fudged the xml file, and the file parsed without any problems???
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

 
reply
    Bookmark Topic Watch Topic
  • New Topic