Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将DefaultListModel封送到xml中_Java_Xml_Jaxb_Jlist_Defaultlistmodel - Fatal编程技术网

Java 将DefaultListModel封送到xml中

Java 将DefaultListModel封送到xml中,java,xml,jaxb,jlist,defaultlistmodel,Java,Xml,Jaxb,Jlist,Defaultlistmodel,我看过一些关于这个主题的帖子,但是没有一个解决方案对我有效。我有一个名为partsListRight的DefaultListModel,它是我的窗口类的一个成员,我有一个名为configuration的内部类,它包含一个ArrayList,我想用partsListRight中的项填充它: 然而,当我尝试运行这个方法时,我得到一个错误,即配置是一个非静态的内部类,JAXB无法处理这些。我不知道该怎么解决这个问题。非常感谢您的帮助 您不需要内部类。我可以看出您这样做的原因是因为您希望访问Defaul

我看过一些关于这个主题的帖子,但是没有一个解决方案对我有效。我有一个名为partsListRight的DefaultListModel,它是我的窗口类的一个成员,我有一个名为configuration的内部类,它包含一个ArrayList,我想用partsListRight中的项填充它:


然而,当我尝试运行这个方法时,我得到一个错误,即配置是一个非静态的内部类,JAXB无法处理这些。我不知道该怎么解决这个问题。非常感谢您的帮助

您不需要内部类。我可以看出您这样做的原因是因为您希望访问DefaultListModel,但这是不必要的。JAXB模型类不必了解任何gui方面的知识。它只是简单地用来建模

也就是说,你的设计有点不对劲。相反,您应该将配置设置为自己的类文件。配置类也不需要构造函数。只需为列表设置一个getter,并从save方法填充它。比如:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "component"
})
@XmlRootElement(name = "Configuration")
public class Configuration {

    protected List<String> component;

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


谢谢你的回复;我试着用这个,现在我有一个新的错误。javax.xml.bind.JAXBException:未能实例化提供程序com.sun.xml.internal.bind.v2.ContextFactory:javax.xml.bind.JAXBException:com.cooksys.assessment不包含ObjectFactory.class或jaxb.index

抱歉,我习惯于使用xjc,在这里您可以创建一个ObjectFactory。在您的情况下,只需使用与以前相同的方法创建JAXBContext,使用Configuration.class而不是Configuration.class.getPackage.getName

试验

结果

注意:如果再次使用当前注释配置,xjc将创建上述注释:-

@XmlRootElement(name = "Configuration")
public class Configuration {

    @XmlElement
    protected List<String> component;

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

它必须是非静态的吗?它必须是一个内部类吗?我想它不必是一个内部类,但它必须是非静态的,因为DefaultListModel取决于在GUIThanks中的JList中为回复选择了哪些项;我试着用这个,现在我有一个新的错误。javax.xml.bind.JAXBException:Provider com.sun.xml.internal.bind.v2.ContextFactory无法实例化:javax.xml.bind.JAXBException:com.cooksys.assessment不包含ObjectFactory.class或jaxb.index-链接异常:[javax.xml.bind.JAXBException:com.cooksys.assessment不包含ObjectFactory.class或jaxb.index]不幸的是,我真的不明白这段代码是如何工作的,我也不知道这个错误意味着什么。就是这样!非常感谢你的帮助。
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "component"
})
@XmlRootElement(name = "Configuration")
public class Configuration {

    protected List<String> component;

    public List<String> getComponent() {
        if (component == null) {
            component = new ArrayList<String>();
        }
        return this.component;
    }
}
Configuration config = new Configuration();
List<String> list = config.getComponent();
Object[] partsList = partsListRight.toArray();
for (Object object : partsList){
    list.add((String)object);
}

JAXBContext context = JAXBContext.newInstance(
                      Configuration.class.getPackage().getName());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
marshaller.marshal(config, System.out);
JAXBContext context = JAXBContext.newInstance(Configuration.class);
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Marshall {

    public static void main(String[] args) throws Exception {
        JAXBContext context = JAXBContext.newInstance(Configuration.class);
        Marshaller marshaller = context.createMarshaller();
        Configuration config = new Configuration();
        List<String> list = config.getComponent();
        list.add("Hello");
        list.add("World");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(config, System.out);
    }
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Configuration>
    <component>Hello</component>
    <component>World</component>
</Configuration>
@XmlRootElement(name = "Configuration")
public class Configuration {

    @XmlElement
    protected List<String> component;

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