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

后端 未结 1 479
遇见更好的自我
遇见更好的自我 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 {
    
        private ObservableList list;
    
        public MyWrapperForList() {
            list = FXCollections.observableArrayList();
        }
    
        public MyWrapperForList(ObservableList list) {
            this.list = list;
        }
    
        @XmlAnyElement(lax = true)
        public ObservableList 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 {
    
        private List list = FXCollections.observableArrayList();
    
        public MyWrapperForList() {
            list = FXCollections.observableArrayList();
        }
    
        public MyWrapperForList(List list) {
            this.list = list;
        }
    
        @XmlAnyElement(lax = true)
        public List getItems() {
            return list;
        }
    
    }
    

    Demo Code

    Input (nub.xml)

    
        
            [none]
            Year
            dfg
            4
        
        
            [none]
            Year
            ROBO
            1234
        
    
    

    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 wrapper = (MyWrapperForList) unmarshaller.unmarshal(xml, MyWrapperForList.class).getValue();
            List 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)
提交回复
热议问题