Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 584
  • Last Modified:

How to serialize an object to XML

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:

<MyObjects>
<MyObject>
      <recordUID>1234</recordUID>
      <userID>me</userID>
      <contentType>something</conentType>
      <tags>
                <tag>tag1</tag>
                <tag>tag2</tag2>
       </tags>
       <timeStam>23:59 2005</timeStamp>
       <data>CDATA here</data>
       <signature> CDATA here</signature>
</MyObject>

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?  
0
jjacksn
Asked:
jjacksn
6 Solutions
 
CEHJCommented:
Use XMLEncoder
0
 
radarshCommented:
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 - http://www.castor.org
2. Digester - http://jakarta.apache.org/commons/digester/
3. XMLBeans - http://java-source.net/open-source/xml-parsers/xmlbeans
________
radarsh
0
 
objectsCommented:
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Manikandan ThiagarajanSenior consultantCommented:
0
 
mrigankCommented:
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.
0
 
mightyoneCommented:
// Serialize the document
      OutputFormat format = new OutputFormat(doc);
      format.setLineWidth(65);
      format.setIndenting(true);
      format.setIndent(2);
      XMLSerializer serializer = new XMLSerializer(System.out, format);
      serializer.serialize(doc);
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now