0

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?

Farid Valiyev
  • 203
  • 5
  • 20

1 Answers1

1

Your methods are defined incorrectly. Look at the javadoc for any JavaFX class and you will see the correct way to do it.

Consider, for example, the Circle class:

private final DoubleProperty radius = new SimpleDoubleProperty();

public DoubleProperty radiusProperty() {
    return radius;
}

public double getRadius() {
    return radius.get();
}

public void setRadius(double value) {
    radius.set(value);
}

(The above is a simplified approximation and is not taken from the actual source for that class.)

Under no circumstances should a get-method return a Property. Under no circumstances should a set-method take a Property argument.

VGR
  • 40,506
  • 4
  • 48
  • 63