File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Creating xml documents dynamically based on a schema 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 "Creating xml documents dynamically based on a schema" Watch "Creating xml documents dynamically based on a schema" New topic
Author

Creating xml documents dynamically based on a schema

Tony Brown
Greenhorn

Joined: Jan 02, 2004
Posts: 6
I have created a java form in Swing that has fields for user input. What I need to do is to get that user input into xml format that conforms to an xml schema I created.
Is it possible to do this with DOM? I saw a createDocumentType function that seems like it might be what I need. Does that sound right? Any help would be greatly appreciated.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
I would just write straight-forward Java code, which takes the input and writes the corresponding XML into a PrintWriter/StringWriter.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Ketan KC Chachad
Ranch Hand

Joined: Nov 23, 2003
Posts: 76
Hi All,
Even I have a similar kind of doubt. I have been using classes from the com.docuverse.dom package for creating a DOM structure for a XML file. But whenever I am creating such a file I need to get the initial root element from an already existing XML file.
In other words I first parse an already existing XML template file using the DOMParser of org.apache.xercers.parsers package. From this I get the Document of the XML file and from there I get the Root Element to which I add the rest of the elements. Can I not create a Document and append elements to it from scratch and then write the whole structure to a file?
Can anyone provide me with any kind of tutorials / links / help that will help me do the same?


Regards,<br />Ketan KC Chachad
Hari Vignesh Padmanaban
Ranch Hand

Joined: Oct 07, 2003
Posts: 578
As lasse mentioned, I tyhink the straigt forward way to do it is to write it using a fileoutputstream as follows !!!

DOM does provide an Interface called DOMImplementation that allows you to create a new Document based on a DTD. You can then add elemnts and attributes using the Document interface. I am not sure if tehre is one for schema .
The methods defined in teh interface are as follows

An example of doing ot using this would be as follows

Hope this helps
hari
Ketan KC Chachad
Ranch Hand

Joined: Nov 23, 2003
Posts: 76
Thank you Hari Vignesh Padmanaban for the code. But can you provide me with some links for that will help me understanding the API more? Some kind of Tutorials of any kind. It will be a big help. Thanks in advance. This request goes to everyone who can help me
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Creating xml documents dynamically based on a schema
 
Similar Threads
build a new xml file
Reading xmlschema file
validating SOAP Message along with the contents of SOAP Body
Is there a java API to generate xml with an inline schema definition
Need Help