This week's book / license giveaways are in the JDBC and Relational Databases and Java in General forums.
We're giving away four copies each of PostGIS in Action and Java Advanced Topics Training and have the authors on-line!
See this thread and this one for details.
The moose likes XML and Related Technologies and the fly likes Validating xml against dtd 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 "Validating xml against dtd" Watch "Validating xml against dtd" New topic
Author

Validating xml against dtd

Vishnu Murthy
Ranch Hand

Joined: Jan 04, 2001
Posts: 56
Hi all
I am new to xml,
I am using DOM parser for xml parsing, before i am going to parse my xml file I want to validating my xml file against the .dtd. could someone tell as how to do this. if possible please give me sample code.
I have written code for parsing xml as below
try{
//Create Document
Document doc = null;
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
DocumentBuilder db = dbf.newDocumentBuilder();
File file = new File(XMLFILE);
doc = db.parse(file);
NodeList nl = doc.getElementsByTagName("ITestFrame");
---statements
--statements
--
--
--
}catch(IOException e){
System.out.println("Caught Exception : "+e);
}
here before going for parsing i want check XMLFILE is according to dtd or not.
do we have any method for that.
Thanks
Vishnu
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
This topic is more appropriate to the xml forum. I'll move it there for you.


Matthew Phillips
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
You don't need to call any method for doing the validation -- the parse(...) method does that behind the scenes if you've configured the parser to do validation.
You can use this sample as a base for your DTD validation. You basically need to just drop the ".../validation/schema" feature.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validating xml against dtd