wood burning stoves
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

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Validating xml against dtd" Watch "Validating xml against dtd" New topic

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
//Create Document
Document doc = null;
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
File file = new File(XMLFILE);
doc = db.parse(file);
NodeList nl = doc.getElementsByTagName("ITestFrame");
}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.
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

Joined: Jan 23, 2002
Posts: 11962
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
It's not a secret anymore!