For a current project, I need to write XML data into a ByteBuffer to write the output XML in a binary format (.dat for example).
Is there a lib or class that will, for each java objects corresponding to the elements of my xml, write the value of its attributes into a ByteBuffer (or some similar implementation) while respecting the attributes type (int -> buffer.putInt(intAttribute) ...
PS: Ideally, I would need this method to put the size of each element sequence before the values of those elements
Edit : Here is a concrete example of what I'd like
Let's say I have the following class defining the walker_template xml element
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"routestep"
})
@XmlRootElement(name = "walker_template")
public class WalkerTemplate {
protected List<Routestep> routestep;
@XmlAttribute(name = "route_id")
protected String routeId;
public List<Routestep> getRoutestep() {
if (routestep == null) {
routestep = new ArrayList<Routestep>();
}
return this.routestep;
}
RouteStep being :
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"value"
})
@XmlRootElement(name = "routestep")
public class Routestep {
@XmlAttribute
protected Integer step;
@XmlAttribute
protected Float x;
@XmlAttribute
protected Float y;
@XmlAttribute
protected Float z;
Those classes defines objects I usually marshal using JAXB then write it into an output file.
marshaller.marshal(templatesToWrite, myXMLStreamWriter);
What I would now want is to write the data of those objects in a binary format.
The method that will do that should look like (for this example) :
ByteBuffer buffer = new ByteBuffer.allocate(size);
buffer.putInt(myWalkerTemplate.getRouteId());
buffer.putInt(myWalkerTemplate.getRouteSteps().size());
for (Routestep step : myWalkerTemplate.getRouteSteps()) {
buffer.putFloat(step.getX());
buffer.putFloat(step.getY());
buffer.putFloat(step.getZ());
}
So, is there any library/Class that could do that automatically for any object, given the classes of those objects of course (like the Marshaller using a JAXBContext).
Thanks