We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How to serialize an object to XML

jjacksn
jjacksn asked
on
Medium Priority
602 Views
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:

<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?  
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
Use XMLEncoder

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Commented:
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
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Manikandan ThiagarajanSenior consultant
Commented:
Commented:
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.
// 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);
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.