Reputation: 31
<PersonalVehicleCoverage>
<EffectiveDate class="sql-date">2011-03-01</EffectiveDate>
<ExpirationDate class="sql-date">2011-05-31</ExpirationDate>
</PersonalVehicleCoverage>
The EffectiveDate is of java.sql.date;
I am using XStream to generate XML from java objects using the following syntax:
xstream.toXML(data);
I don't want class="sql-date"
as output in the generated XML.
How do I achieve that?
Upvotes: 2
Views: 1362
Reputation: 5492
This is what helped me resolve the same problem:
xstream.addDefaultImplementation(java.sql.Date.class, Date.class);
xstream.addDefaultImplementation(java.sql.Timestamp.class, Date.class);
Where Date.class
is java.util.Date
.
Upvotes: 2
Reputation: 3592
To achieve what you want is straightforward.
You create an XStream instance and configure it (in the example below I have to set the alias for the PersonalVehicleCoverage
as static inner classes get serialized with the prefix of the containing class.
As the example does not use a package, it is serialized as you required. If your classes are in a package, you can use something like this to adapt the XML: xStream.aliasPackage("pre", "my.package");
Here is the sample code:
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import com.thoughtworks.xstream.XStream;
public class XStreamDemo {
public static void main(String[] args) throws ParseException {
XStream xStream = new XStream();
xStream.alias("PersonalVehicleCoverage", PersonalVehicleCoverage.class);
PersonalVehicleCoverage object = new PersonalVehicleCoverage();
DateFormat dateFormat = new SimpleDateFormat("yyyy MMM DD");
object.EffectiveDate = new Date(dateFormat.parse("2011 Jan 1").getTime());
object.ExpirationDate = new Date(dateFormat.parse("2011 Jan 31").getTime());
System.out.println(xStream.toXML(object));
}
static class PersonalVehicleCoverage {
Date EffectiveDate;
Date ExpirationDate;
}
}
And here is the output of that example:
<PersonalVehicleCoverage>
<EffectiveDate>2011-01-01</EffectiveDate>
<ExpirationDate>2011-01-31</ExpirationDate>
</PersonalVehicleCoverage>
Upvotes: 0