How to generate a DTD file with java?

I'd recommend that you use an XML schema . Xsd instead of a DTD.

Up vote 0 down vote favorite share g+ share fb share tw.

I am not new in the java but new in XML and DTD files. I have a tool(I wrote) that saves the projects as XML. You can create applications from the main project but I have to check validity of the applications against the main project.

So with every main project I have to create a DTD file as well as the XML files. So that I can check the applications(XML files also) againts that DTD file. How I can generate that dtd file with Java.

Java xml dtd link|improve this question asked Feb 27 at 20:45fuatbasik12.

If you just generate it, the DTD will be useless, it won't be checking for anything useful. Sounds like all your project XML files should validate against the same DTD. – Juan Mendes Feb 27 at 20:48.

I'd recommend that you use an XML schema . Xsd instead of a DTD. It has several advantages over a DTD: An .

Xsd is XML; a DTD is not. A schema will do more thorough checking than a DTD can. It can enforce types, regular expressions, enumerations, etc. A smart IDE like IntelliJ can generate a schema for you from an existing XML and visa versa.

A schema is easy to generate with a text editor. I think it's a worthwhile exercise. I wouldn't recommend generating such a thing in any case.

You're expressing a lot of knowledge about the document in the schema or DTD.

The way I'd do it is using JAXB objects and annotations, and XSD instead a DTD. Create JAXB Java objects with annotations representing the structure of your XML, write ("marshall") to a file using a Marshaller, use schemagen to generate a XSD from the JAXB objects and their annotations. As a bonus, JAXB will allow you to read back the XML into your object structure.

DTDs often get a bad rap, and often for poorly-considered reasons such as what one might call "developer fad and fashion" (e.g. , the latest thing is the best thing). For example, it's rather odd to think that DTDs are not XML. I think people often mean that the syntax of a DTD is not XML document markup syntax, which is correct.

But DTDs are intrinsically XML, i.e. , they are very much part of the XML Recommendation, certainly moreso than XML Schema, which like XML Namespaces came later as one of a large number of add-on Recommendations, which raises the thorny question "what is a compliant XML document? " But we won't touch that one here.

What is certain is that every compliant XML processor is required to support the DOCTYPE declaration and DTD processing; it's free, you don't need anything else. And projects such as the Apache Catalog Resolver provide support for both TR 9401 and XML Catalogs ("Entity Resolution"), which has a long history of industry support in both open source and commercial products. By contrast, XML Schema is not supported by all XML processors (or at least consistently so), and the means by which an XSD is linked to the document instance is handled via a number of rather convoluted XML Namespace attributes that are part of the document instance (attributes on the root element), not part of the prolog.

(Basically, this means processors have to begin processing the document prior to obtaining the schema. If you don't understand the importance of this distinction you should do some homework. ) The interdependencies within hybrid document types using multiple XSDs is complicated and underspecified.

You may find you have to provide your own support for entity resolution. And having your schemas in XML markup is not always a benefit, unless you plan to manage them via software tools. DTDs are much more human readable, and despite some contrary opinions you can use namespace prefixes (see XHTML Modularisation).

If your validation requirements are mostly structural (e.g. , your content is document-centric rather than data-centric) you probably aren't trying to validate the content, but rather the markup. DTDs are fine for that. You might also look into entirely different approaches to validation and document constraints, such as Schematron, which augments DTD validation very well.

Finally, most tools that can generate XSDs can also generate DTDs, and there are conversion applications available (see James Clark's trang). DTDs will also be around as long as XML, say 50-100 years. In the long term we may find that XML Schema won't survive: it is just one amongst a number of viable competitors, competitors that arrived on the scene because of the many failings of XSDs.

I find that a combination of DTDs and Schematron is pretty much ideal for my needs, and is a flexible solution. But YMMV. In summary, you really should look at your actual schema/constraint requirements before accepting generic, scatter-shot advice.

You may find DTDs work very well. They have done so since around 1971.

... DTDs are much more human readable..." - your opinion; I disagree. And no one knows if XML will survive 50-100 years. More speculation.

Processing schemas is built into the Java JDK now, so your statement about consistent support isn't correct anymore. DTDs might have been around since before XML, but the XML spec itself dates back to 1998: w3.org/2003/02/xml-at-5.html – duffymo Feb 27 at 23:45.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions