File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes XML Validation using the java API Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML Validation using the java API" Watch "XML Validation using the java API" New topic

XML Validation using the java API

Alfred Patino

Joined: Sep 26, 2002
Posts: 3
can any body tell me how to validate a XML file while parsing with an DTD file,I m getting the XML file dynamically ,so i m not getting the DOCTYPE line in the XML and i dont wanna include that.
so is there any facility so seperately mention the DTD File???
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1183
Before a document can be validated against a DTD, the document itself must contain a document type declaration pointing to the DTD it should be validated against. You cannot easily receive a document from a third party and validate it against your own DTD. You have to validate it against the DTD that the document's author specified. This is excessively limiting.
For example, imagine you're running an e-commerce business that accepts orders for products using SOAP or XML-RPC. Each order comes to you over the Internet as an XML document. Before accepting that order the first thing you want to do is check that it's valid against a DTD you've defined to make sure that it contains all the necessary information. However, if DTDs are all you have to validate with, then there's nothing to prevent a hacker sending you a document whose DOCTYPE declaration points to a different DTD. Then your system may report that the document is valid according to the hacked DTD, even though it would be invalid when compared to the correct DTD. If your system accepts the invalid document, it could introduce corrupt data that crashes the system or lets the hacker order goods they haven’t paid for, all because the person authoring the document got to choose which DTD to validate against rather than the person validating the document.
Schemas are more flexible. The schema specification specifically allows for a variety of different means for associating documents with schemas. For instance, one possibility is that both the name of the document to validate and the name of the schema to validate it against could be passed to the validator program on the command line like this:
C:\>validator greeting.xml greeting.xsd


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Pradeep Sahoo
Ranch Hand

Joined: Oct 13, 2002
Posts: 46
I am also trying to validate an input XML to my servlet against XML Schema.
If anybody could please tell me how to do it in Java. I came to know that it can be done only through the parsers which support JAXP version 1.2.
If anybody can suggest me which version of Xerces supports the above specifications.
Thanks in advance,

Pradeep<br />Consultant - Enterprise Solutions,<br />Infosys Technologies Ltd,<br />Bangalore, India
Alfred Patino

Joined: Sep 26, 2002
Posts: 3
hi i know that validation of XML can be done against a DTD when the DOCTYPE declaration(DTD path) is inside an XML .
But my problem is that i m getting the XML from some 3rd party and i want to process that only if it is validated by my DTD .
so i dont have the DOctype declaration in mY XML
please help in in knowing how can i validate the XML against the DTD in this case
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
How about creating a new file that concatenates the DOCTYPE declaration and the actual input file, and then validating that?

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
I agree. Here's the link:
subject: XML Validation using the java API
It's not a secret anymore!