It's not a secret anymore!*
The moose likes XML and Related Technologies and the fly likes How to validate XML against DTD / Schema using DOM 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 "How to validate XML against DTD / Schema using DOM" Watch "How to validate XML against DTD / Schema using DOM" New topic
Author

How to validate XML against DTD / Schema using DOM

Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
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

Joined: Nov 12, 2001
Posts: 906
Hi,
You can do it using Xerces features for instance.

For further information about features :
http://xml.apache.org/xerces-j/features.html
HTH


/ JeanLouis<br /><i>"software development has been, is, and will remain fundamentally hard" (Grady Booch)</i><br /> <br />Take a look at <a href="http://www.epfwiki.net/wikis/openup/" target="_blank" rel="nofollow">Agile OpenUP</a> in the Eclipse community
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
This is also illustrated in our FAQ page for HowToValidateXmlAgainstSchema.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Mar 18, 2004
Posts: 572
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

Joined: Aug 26, 2001
Posts: 7610
Moving this thread to the XML forum. Please continue the discussion there. Thank you


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Nov 12, 2001
Posts: 906
The way to specify an external schema is something like that :
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to validate XML against DTD / Schema using DOM