Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing xml to a file

Status
Not open for further replies.

marouanemeft

Programmer
Sep 29, 2004
10
0
0
US
Hi,
I have 4 or 5 classes that are related to each other this way:
Installation class has domains
Domain class has users
User class has objects ..

I would like to write a method that will write an xml representation of this containership to a file.

right now I have a writeXML method that returns a string for each object. and the writeXML of the top classes (like Installation) just call the writeXML of the subclasses (like User).

Is there a better way to do this?

Thank you all in advance.

 
If your classes conform to the JavaBeans specification then you have a very easy option in using the java.beans.XMLEncoder class to write the top-most class to file. All the contained classes will be included in the resulting file. The XMLEncoder class has a sample code snippet to get you started. You use the corresponding XMLDecoder class to re-create your User classes from previously written files.

The resulting XML is more aimed at object persistence than human readablility, however.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Now who's going 'one better'... [wink]

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top