Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to validate XML against DTD / Schema using DOM

 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
Can anyone tell me how can i validate xml file against DTD/Schema using DOM or jDOM? I mean to say that user will provide me the path of the xml file to parse, I will validate that xml file using the DTD/Schema i have.
My Question is how can i validate that XML file against the DTD/Schema i have. Please provide code or any tutorial from internet.
Please reply ASAP.
Thanks and Regards
[ April 05, 2004: Message edited by: Ali Gohar ]
 
JeanLouis Marechaux
Ranch Hand
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You can do it using Xerces features for instance.

For further information about features :
http://xml.apache.org/xerces-j/features.html
HTH
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is also illustrated in our FAQ page for HowToValidateXmlAgainstSchema.
 
JeanLouis Marechaux
Ranch Hand
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bad thing is, as far as I know, that you cannot validate a in-memory DOM element.
This feature only come with DOM 3.
So if you have an DOM argument, you have to serialize it before validating its XSD compliance
(Lasse, please correct me if I am wrong)
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jean-Louis Marechaux:
The bad thing is, as far as I know, that you cannot validate a in-memory DOM element.
I guess this is true. There is no in-memory implementation of InputSource that you could pass on to DocumentBuilder#parse()... Although you could try to write such a class.
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all of you. But my question was that how can i validate the XML file supplied by user against the Schema file that i have. I mean lets suppose i have set the Features. But there isn't any Schema file i have supplied it will validate against the Schema file supplied in that XML file.Isn't it?
Can i validate the file using the Schema that i have, Not provided by the user. User will only provide the XML Document. Is it possible?
Once again thanks for all of you.
[ April 05, 2004: Message edited by: Ali Gohar ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this thread to the XML forum. Please continue the discussion there. Thank you
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ali Gohar:
Can i validate the file using the Schema that i have, Not provided by the user. User will only provide the XML Document. Is it possible?
We have an example at our wiki (although the page in question is currently not rendered correctly) where you can specify an "external schema document" to validate against using Xerces-specific properties. There's also a very nice article by Elliotte Rusty Harold at InformIT.com about the subject.
If (when) you're able to use JAXP 1.2, you can also use a standard mechanism for telling the parser where to find the schema document.
 
JeanLouis Marechaux
Ranch Hand
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way to specify an external schema is something like that :
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic