Search code examples
javaxmljavabeansxstreamxmlencoder

Drop-in replacement for java.beans.XMLEncoder


I've got lots XSL transforms that rely on the java.beans.XMLEncoder XML format, and I'm wondering if I can find a drop-in replacement lib that has better performance. I've looked at XStream but its serialization format is different.

I'm looking to replace it as I'm working with a legacy codebase that has a forked version of XMLEncoder and I'd like to return it to something more standard, but java.beans.XMLEncoder has much worse performance.

For a class Person (with appropriate getters and setters):

public class Person {
    private String name;
    private List<String> favoriteColors;
    private Date birthDate;
    private int age;
}

XMLEncoder produces XML like the following:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.8.0_66" class="java.beans.XMLDecoder">
 <object class="Person" id="Person0">
  <void property="age">
   <int>40</int>
  </void>
  <void property="birthDate">
   <object class="java.util.Date">
    <long>175064400000</long>
   </object>
  </void>
  <void property="favoriteColors">
   <void method="add">
    <string>red</string>
   </void>
   <void method="add">
    <string>green</string>
   </void>
  </void>
  <void property="name">
   <string>John Doe</string>
  </void>
 </object>
</java>

I'm guessing it would be possible to create a bunch of XStream Converters to approximate the format but I'm wondering if there's an easier solution before I head down that rabbit hole.


Solution

  • In case anyone else runs across this later, implementing via XStream will get you there as long as you don't care about the order of elements. You could probably do a deeper implementation that allows you to control the order of the elements, but I ended up scrapping the idea and I'm just going to deal with it until I can re-write the code in question.