0
I’m developing a framework that manages system calls and needs to save/load the state of the instance variables in a specific XML format.
I looked for tools on the Internet and found the JAXB 2
. This tool allows you to map a XML Schema
for Java classes, however I already own the classes and want to do a one-to-one match with the expected XML format.
Below are the three classes of framework:
public class Experiment {
private String name;
private ArrayList<Task> tasks;
public Experiment(String name, ArrayList<Task> tasks) {}
public Experiment(String name, Task ... tasks) {}
}
public class Requirements {
private int cores;
private int ram;
private int storage;
private long timeout;
public Requirements(int cores, int ram, int storage, int timeout) {}
}
public class Task implements Runnable {
private String id;
private String command;
private List<Task> dependencies;
private Requirements requirements;
private List<Task> observers;
public Task(String id, String command) {}
}
Example of a XML
expected:
<?xml version="1.0" encoding="UTF-8" ?>
<experiment>
<name>Test</name>
<vars>
<var>
<id>message</id>
<value>Hello World!!!</value>
</var>
<var>
<id>string</id>
<value>String test</value>
</var>
</vars>
<tasks>
<task>
<id>print</id>
<command>echo $(message)</command>
<dependencies/>
<requirements>
<cores>1</cores>
<ram>0</ram>
<storage>0</storage>
<timeout>0</timeout>
</requirements>
</task>
</tasks>
</experiment>
Schema
generated from the example by IntelliJ
:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="experiment" type="experimentType"/>
<xs:complexType name="varType">
<xs:sequence>
<xs:element type="xs:string" name="id"/>
<xs:element type="xs:string" name="value"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="varsType">
<xs:sequence>
<xs:element type="varType" name="var" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="requirementsType">
<xs:sequence>
<xs:element type="xs:string" name="cores"/>
<xs:element type="xs:string" name="ram"/>
<xs:element type="xs:string" name="storage"/>
<xs:element type="xs:string" name="timeout"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="taskType">
<xs:sequence>
<xs:element type="xs:string" name="id"/>
<xs:element type="xs:string" name="command"/>
<xs:element type="xs:string" name="dependencies"/>
<xs:element type="requirementsType" name="requirements"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="tasksType">
<xs:sequence>
<xs:element type="taskType" name="task"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="experimentType">
<xs:sequence>
<xs:element type="xs:string" name="name"/>
<xs:element type="varsType" name="vars"/>
<xs:element type="tasksType" name="tasks"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Can someone explain to me how to convert an object Experiment
for XML and vice versa?
You can create your own parse and write the XML tags with the name you want. https://examples.javacodegeeks.com/core-java/xml/parsers/documentbuilderfactory/create-xml-file-in-java-using-dom-parser-example/
– Marcus Becker
Thanks for the answer. I already have a "manual" solution like this, but it is not feasible for me since the chance of schema changes is high. I don’t want to have to change code every time a new change appears.
– Michael Pacheco
Would using annotations help your case? For example: https://github.com/marcusbecker/NodeThunder/wiki/Using-annotations
– Marcus Becker
I did that last night. I followed the tutorial on http://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v2.2/example_schemagen_basic.html But I have a little problem. I’ll ask another question to try to solve it.
– Michael Pacheco
The new question: https://answall.com/questions/356276/comort-annotater-propedades-java-do-typo-map-utilizando-o-jaxb2
– Michael Pacheco
You can use the Jackson XML parser. This article explains in detail how https://www.baeldung.com/jackson-xml-serialization-and-deserializationworks
– Paulo Souza