How to serialize JavaFX Nodes preferably in XML?

2019-08-06 04:31发布

I have some kind of drawpane, where the user can draw different kinds of Shapes, like Pathes, Rectangles, Texts, etc. Now I´d like to persist the DrawState, so that I can recreate it at any time.

I just tried it with xstream, but like usual I get a whole lot of dependencies within these Shapes when persisting it in XML. After some research I found this Thread here:

Serialize JavaFX components

I liked the idea of jewelsea using the SceneBuilder API to serialize my shapes, since they are all supported and the FXML contains all the things I probably will need without any disturbing references.

Do you have any experiences with the Scenebuilder API? In the thread its pointed out, that it might be difficult to get only the things needed. Or can you maybe recommend any other way to serialize Shapes in XML?

2条回答
虎瘦雄心在
2楼-- · 2019-08-06 04:46

Using annotations to describe what data to save, JAXB makes it very simple to save/load objects to/from XML.

You'll probably want to create template objects for each JavaFX object so you can control what data you are storing. Here's a tutorial on using JAXB.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-08-06 04:56

This might come a bit late, but if you use XStream, note that we shared a collection of Converters for JavaFX types: see https://github.com/dooApp/XStreamFX

查看更多
登录 后发表回答