Sangita Gupta wrote:
>I wish to write a java code which can generate an xml document based on
>a given dtd. Walk through the dtd, know which tags (name) to include in
>the xml document, grab the value froma string and generate the document.
>Is it possible? Any example will be greatly appreciated.
I have been researching this also and there would seem to be a lot of demand
since lots of corporate data is in flat files. I have basically seen three
approaches:
1. Create a java application that reads in your DTD, reads in your data (a
line at a time) and then creates the internal DOM structure using those two
sources. Once you have the internal DOM built, you can serialize the XML as
an output stream.
Advantages - everything is done in one program
Disadvantages - fairly complex and dependent upon a specific implementation
of parser (IBM xml4j version 2 API)
Take a look at "XML and Java Building Web Applications" by Maruyama, Tamura
and Uramoto...chapter 3.
2. (courtesy of Doug Tidwell)
a simpler approach would be a Java application that reads in your data and
simply creates XML tags associated with each column. Somthing like this,
<?xml version="1.0"?>
<document>
<row>
<column 1>xxxxx</column1>
<column 2>xxxxx</column2>
.....
</row>
....more rows....
</document>
then, you use xalan and stylesheets to "render" this XML format into a
format suitable for your particular application, where you do conversion of
columns to actual meaningful tags:
...
<employees>
<employee sex="F">
<serial number>000000</serial number>
<name>
<first_name>john</first_name>
<last_name>doe</last_name>
</name>
.....
the last step is to run it against the parser to validate against your DTD
Advantage - much simpler and parser independence
Disadvantage - more programs/steps/coordination required
3. buy from a vendor (I am researching this but have not found much).
Tom Watson