I use JAXB (Java Architecture for XML Binding) convert Java
object to XML
file
JAXBContext context = JAXBContext.newInstance(MetaListWrapper.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// Wrapping my data.
MetaListWrapper wrapper = new MetaListWrapper();
wrapper.setTree(sectionList);
// Marshalling and saving XML to the file.
m.marshal(wrapper, file);
This is my Wrapper:
@XmlRootElement(name = "metadata")
public class MetaListWrapper {
private List<Section> sectionList;
@XmlElement(name = "sectionList")
public List<Section> getTree() {
return sectionList;
}
public void setTree(List<Section> sectionList) {
this.sectionList = sectionList;
}
}
And this is Section object:
public class Section {
List<Theme> themes;
private SimpleStringProperty name, description;
@Override
public String toString() {
return name.toString();
}
public List<Theme> getThemes() {
return themes;
}
public void setThemes(List<Theme> themes) {
this.themes = themes;
}
public SimpleStringProperty getName() {
return name;
}
public void setName(SimpleStringProperty name) {
this.name = name;
}
public SimpleStringProperty getDescription() {
return description;
}
public void setDescription(SimpleStringProperty description) {
this.description = description;
}
}
When I use private String name, description;
in the object (Section and Theme) JAXB
extract XML
normally. XML
look like this:
<sectionList>
<name>Section 1</name>
<themes>
<name>Theme 1</name>
</themes>
</sectionList>
But when I use private SimpleStringProperty name, description;
return null; and XML
look like this:
<sectionList>
<name/>
<themes>
<name/>
</themes>
</sectionList>\
I must use SimpleStringProperty
in the project. Whats wrong? How can I extract SimpleStringProperty
to XML
?