Marshall a List to XML works - but how to unmarshall?

后端 未结 1 482
遇见更好的自我
遇见更好的自我 2021-01-25 06:43

I can marshall a ObservableList using a \"Wrapper\"-class like below. But I cannot unmarshall it back to the wrapperclass it was before.

The idea is: I have an Observabl

相关标签:
1条回答
  • 2021-01-25 06:59

    MyListWrapper

    If you want MyWrapperForList to unmarshal holding an instance of ObservableList then you will need to setup your class in one of the following ways.

    Property of Type ObservableList

    import javax.xml.bind.annotation.XmlAnyElement;
    import javafx.collections.*;
    
    public class MyWrapperForList<T> {
    
        private ObservableList<T> list;
    
        public MyWrapperForList() {
            list = FXCollections.<T>observableArrayList();
        }
    
        public MyWrapperForList(ObservableList<T> list) {
            this.list = list;
        }
    
        @XmlAnyElement(lax = true)
        public ObservableList<T> getItems() {
            return list;
        }
    
    }
    

    List Property Initialized to Instance of ObservableList

    import java.util.List;
    import javax.xml.bind.annotation.XmlAnyElement;
    import javafx.collections.*;
    
    public class MyWrapperForList<T> {
    
        private List<T> list = FXCollections.<T>observableArrayList();
    
        public MyWrapperForList() {
            list = FXCollections.<T>observableArrayList();
        }
    
        public MyWrapperForList(List<T> list) {
            this.list = list;
        }
    
        @XmlAnyElement(lax = true)
        public List<T> getItems() {
            return list;
        }
    
    }
    

    Demo Code

    Input (nub.xml)

    <List>
        <root>
            <category>[none]</category>
            <period>Year</period>
            <title>dfg</title>
            <value>4</value>
        </root>
        <root>
            <category>[none]</category>
            <period>Year</period>
            <title>ROBO</title>
            <value>1234</value>
        </root>
    </List>
    

    Demo

    import java.util.List;
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Unmarshaller;
    import javax.xml.transform.stream.StreamSource;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(MyWrapperForList.class, Expense.class);
    
            //UNMARSHALLING
            Unmarshaller unmarshaller = jc.createUnmarshaller();
    
            StreamSource xml = new StreamSource("src/forum18594548/nub.xml");
            MyWrapperForList<Expense> wrapper = (MyWrapperForList<Expense>) unmarshaller.unmarshal(xml, MyWrapperForList.class).getValue();
            List<Expense> data = wrapper.getItems();
    
            System.out.println(data.getClass());
            for(Expense expense : data) {
                System.out.println(expense);
            }
       }
    
    }
    

    Output

    class com.sun.javafx.collections.ObservableListWrapper
    forum18594548.Expense@789df61d
    forum18594548.Expense@4a8927c8
    

    UPDATE

    First: Thanks for you work Blaise!! I'm really glad for what you do to me! I tried what you wrote here (it was nearly the same as I had) and I got a similar (same type of) output as you got. BUT the objects in the lists are all referenced with null. If I write System.out.println(data.get(0).getTitle()); it says null. There is the exact amount of objects in the list, but all attributes are referenced with null. :(

    I think I got tunnel vision on the ObservableList aspect only to miss your real problem was with how you mapped the Expense class. Since you only have get methods you should map to the fields using @XmlAccessorType(XmlAccessType.FIELD) as follows.

    import javax.xml.bind.annotation.*;
    
    @XmlRootElement(name="root")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Expense {
    
        private String title;
        private String category;
        private String period;
        private String value;
    
        public Expense() {
        }
    
        public Expense(String title, String value, String period, String category) {
            this.title = title;
            this.value = value;
            this.period = period;
            this.category = category;
        }
    
        public String getTitle() {
            return this.title;
        }
    
        public String getCategory() {
            return this.category;
        }
    
        public String getPeriod() {
            return this.period;
        }
    
        public String getValue() {
            return this.value;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题