How to serialize an object to XML

Posted on 2006-03-24
Last Modified: 2010-03-31
I want to serialize an object to XML (actually, a list of objects over time).  the file will be append only (the list of objects can only grow).  

my object looks like this:  

public class MyObject{
     public String recordUID;
     public String userID;
     public String contentType;
     public String[] tags;
     public Date timeStamp;
     public String data;
     public String signature;

and I want to get an xml file that looks like this:

       <timeStam>23:59 2005</timeStamp>
       <data>CDATA here</data>
       <signature> CDATA here</signature>

are there any good ways to do this other than by hand?  and If I do do it by hand, which xml writer should I use?  
Question by:jjacksn
    LVL 86

    Accepted Solution

    Use XMLEncoder
    LVL 10

    Assisted Solution

    I guess you can go for the solution CEHJ suggested in your previous question,
    "XML Encoding / Deconding" if you don't care about the tag names.

    However, if you want to define the tag names yourself, you can try any of

    1. Castor -
    2. Digester -
    3. XMLBeans -
    LVL 92

    Assisted Solution

    LVL 8

    Assisted Solution

    LVL 5

    Assisted Solution

    I had one question. What do you mean by file shall be append only.
    once I create a xml file and save it, further invocation should only append data and not recreate a new file ?

    If that is the case, then I think you need to read the xml file into java objects, add the objects that you want to add and then save the  entire object group back into the xml file.
    LVL 6

    Assisted Solution

    // Serialize the document
          OutputFormat format = new OutputFormat(doc);
          XMLSerializer serializer = new XMLSerializer(System.out, format);

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now