Swing JList - read and write XML from file

537 views Asked by At

I've looked at a few posts on this topic, but none of the solutions have worked for me. I have an load method: The “Load” event should clear the right panel, and call your load() method. When load() is called, it should display the unmarshalled data in the right panel.

public void load() throws Exception {
JAXBContext jaxbContext = JAXBContext.newInstance(Configuration.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Configuration config = (Configuration) jaxbUnmarshaller.unmarshal(new File("save.xml"));
    System.out.println(config.getComponent());

}

So, the question is how do you load the contents of an XML file into a JList ?

Here my Configuration.java

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlAccessType;

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "", propOrder = {
        "part"
})
@XmlRootElement(name = "Configuration")
public class Configuration {

    protected List<String> part;

    public List<String> getComponent() {
        if(part == null) {
            part = new ArrayList<String>();
        }
        return this.part;
    }
}

I have the save method to work correctly and below is my java code. Hope this help

public void load() throws Exception {

    }

    public void save() throws Exception {
        JAXBContext context = JAXBContext.newInstance(Configuration.class);
        Marshaller marshaller = context.createMarshaller();
        Configuration config = new Configuration();
        File file = new File("save.xml");
        List<String> printList = config.getComponent();
        for(int i = 0;i < list.getSize();i++) {
            printList.add((String) list.getElementAt(i));
        }
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(config, file);
0

There are 0 answers