• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

How to validate XML against DTD / Schema using DOM

 
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 906
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this thread to the XML forum. Please continue the discussion there. Thank you
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way to specify an external schema is something like that :
 
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic