I'm working with JAXB and need to generate a XML code like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><!--Created with JFLAP 6.4.-->
<structure>
<type>fa</type>
<automaton>
<!--The list of states.-->
<state id="0" name="q0">
<x>160.0</x>
<y>151.0</y
<initial/> <!-- This what I want-->
</state>
<state id="1" name="q1">
<x>369.0</x>
<y>94.0</y>
<final/> <!-- This what I want-->
</state>
<!--The list of transitions.-->
<transition>
<from>0</from>
<to>1</to>
<read>a</read>
</transition>
</automaton>
</structure>
As you can see, I wanna know how to create a simple @XmlElement without @XmlAttribute, but in my code, I got:
private boolean initial = false;
private boolean final = false;
@XmlElement(name="initial")
public void setInitial(boolean val) {
this.initial = val;
}
@XmlElement(name="final")
public void setFinal(boolean val) {
this.final = val;
}
This way, I got a XML like this:
<state id="0" name="q0">
<x>0.0</x>
<y>0.0</y>
<final>false</final>
<initial>true</initial>
</state>
<state id="1" name="q1">
<x>0.0</x>
<y>0.0</y>
<final>true</final>
<initial>false</initial>
</state>
Does anyone knows how to do it?
You could do the following and leverage an XmlAdapter to convert a Boolean to an empty object to get the desired XML representation.
Root
The property is boolean, but we will make the field Boolean so we can apply an XmlAdapter to it. We will also specify that we want JAXB to map to the field (see: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html).
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
@XmlJavaTypeAdapter(BooleanAdapter.class)
private Boolean foo = false;
@XmlJavaTypeAdapter(BooleanAdapter.class)
private Boolean bar = false;
public boolean isFoo() {
return foo;
}
public void setFoo(boolean foo) {
this.foo = foo;
}
public boolean isBar() {
return bar;
}
public void setBar(boolean bar) {
this.bar = bar;
}
}
BooleanAdapter
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class BooleanAdapter extends XmlAdapter<BooleanAdapter.AdaptedBoolean, Boolean> {
public static class AdaptedBoolean {
}
@Override
public Boolean unmarshal(AdaptedBoolean v) throws Exception {
return null != v;
}
@Override
public AdaptedBoolean marshal(Boolean v) throws Exception {
if(v) {
return new AdaptedBoolean();
} else {
return null;
}
}
}
Below is some demo code that you can run to see that everything works.
Demo
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Root foo = new Root();
foo.setFoo(true);
foo.setBar(false);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(foo, System.out);
}
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<foo/>
</root>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With