【问题标题】:Marshal DefaultListModel into xml将 DefaultListModel 编组为 xml
【发布时间】:2014-11-11 21:52:19
【问题描述】:

我查看了有关此主题的一些帖子,但没有一个解决方案对我有用。我有一个名为partsListRightDefaultListModel,它是我的窗口类的成员,我有一个名为配置的内部类,其中包含一个ArrayList,我想用partsListRight 中的项目填充它:

@XmlRootElement
class Configuration{
    @XmlElement
    private ArrayList<String> Component;


    public Configuration(){
        Component = new ArrayList<String>();
        Object[] partsList = partsListRight.toArray();
        for (Object object : partsList){
            Component.add((String)object);
        }
    }
}

在我的窗口类中,我有一个 save() 方法对 ArrayList 组件进行编组:

public void save() {


    Configuration configuration = new Configuration();

    try{
    File file = new File("C:\\configuration.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(Configuration.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    jaxbMarshaller.marshal(configuration, file);
    jaxbMarshaller.marshal(configuration, System.out);
    } catch(JAXBException e){
        e.printStackTrace();
    }       
}

但是,当我尝试运行此方法时,我收到配置是非静态内部类的错误,而 JAXB 无法处理这些错误。我不知道如何解决这个问题。非常感谢任何帮助!

【问题讨论】:

  • 它必须是非静态的吗?它必须是一个内部类吗?
  • 我猜它不一定是内部类,但它必须是非静态的,因为 DefaultListModel 取决于在 GUI 的 JList 中选择了哪些项目

标签: java xml jaxb jlist defaultlistmodel


【解决方案1】:

您不需要内部类。我可以看到您这样做的原因是因为您想访问DefaultListModel,但这是不必要的。您的 JAXB 模型类不必了解任何有关 gui 方面的信息。它只是为了建模。

也就是说,你的设计有点不对劲。相反,您应该将Configuration 设为自己的类文件。您也不需要 Configuration 类的构造函数。只需为List 设置一个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;
    }
}

然后,当您真正想要进行编组时,只需创建 Configuration 类,并用列表模型元素填充它。

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);

更新

感谢您的回复;我尝试使用它,现在我遇到了一个新错误。 javax.xml.bind.JAXBException:无法实例化提供程序 com.sun.xml.internal.bind.v2.ContextFactory:javax.xml.bind.JAXBException:“com.cooksys.assessment”不包含 ObjectFactory.class 或 jaxb。索引...

抱歉,我习惯于使用 xjc 来为您创建 ObjectFactory。在您的情况下,只需像以前一样创建JAXBContext,使用Configuration.class,而不是Configuration.class.getPackage().getName();

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>

注意:使用您当前的注释配置将是相同的(再次,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;
    }
}

【讨论】:

  • 感谢您的回复;我尝试使用它,现在我遇到了一个新错误。 javax.xml.bind.JAXBException:无法实例化提供程序 com.sun.xml.internal.bind.v2.ContextFactory:javax.xml.bind.JAXBException:“com.cooksys.assessment”不包含 ObjectFactory.class 或 jaxb。索引 - 带有链接异常:[javax.xml.bind.JAXBException:“com.cooksys.assessment”不包含 ObjectFactory.class 或 jaxb.in​​dex] 不幸的是我真的不明白这段代码是如何工作的,我也不知道这个错误意味着什么。
  • 做到了!非常感谢您的帮助。
猜你喜欢
  • 2013-10-09
  • 1970-01-01
  • 2021-04-28
  • 1970-01-01
  • 2023-03-23
  • 2022-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多