views:

1241

answers:

2

I have a simple DTD file with elements and attributes specifications. Can i create a suitable XML file (with DOM) parsing this DTD with Java and no external APIs?

Thanks in advance

+1  A: 

There's some misunderstanding. You can't parse DTD using DOM or SAX. DTD describes document structure. You can validate generated document against DTD using various tools.

Just read and understand the DTD and generate XML document accordingly. You can later check if the document conforms to DTD.

EDIT: You don't have to write code for parsing DTD. First, you should read about DTD. Start with Wikipedia description, there're more links at the end.

Then read DTD manually and understand required document structure. Then just generate XML that will conform to the DTD.

Eugene Morozov
I mean to use DOM only to create XML.. but i don't know it there are good method to parse DTD
Emilio
You don't need to parse DTD. You can later validate generated XML against DTD using tools discussed here, for example: http://bytes.com/groups/xml/86466-checking-xml-dtd-syntax-validating-xml
Eugene Morozov
If you understand DTD and generate a valid document, there's no need to check it against DTD every time. Just make sure than you understand DTD and your document will always conform to it.
Eugene Morozov
I have no XML yet. I have to create it from DTD.
Emilio
You don't create XML from DTD. DTD can't be used in any meaningful way when generating XML. You have to read it with your eyes.Later you can validate XML against DTD if you need to be sure that it conforms.
Eugene Morozov
+1  A: 

I understand your question differently from Eugene Morozov. Tell me if I'm right: you want a program which takes as input a DTD and automatically generates XML file(s) whch are valid according to this DTD. Is it a correct description?

If so, it is certainly possible, such programs exist for other "structure languages" (see abnfgen for BNF grammars; in the XML world, it seems that XMLSpy, for instance, can open a W3C Schema file - XSD file - and generates a sample valid XML) but I don't know one for DTD/XML, you'll have to write it yourself.

Just be aware that there is an infinity of valid XML files according to a given DTD. Your program will just generate a small subset (but it may be sufficient for your purpose: what is it, anyway?)

bortzmeyer
Did you also know that there exist DTDs for which no valid XML could be formed? Good trivia!
Overflown
Post an example, that will be fun.
bortzmeyer
Thank you for the exaustive answer =)
Emilio