【问题标题】:Marshalling an array from JAVA to XML将数组从 JAVA 编组为 XML
【发布时间】:2015-08-27 21:49:39
【问题描述】:

我有一个可以选择的 pc 部件列表,然后移动到右侧以创建列表。在文件菜单中,我需要能够保存,它应该编组 rightList 的内容并将其保存为 XML 文件。我已经尝试了我能想到的一切,但没有任何效果。请帮忙。

这是我的 Window.java 文件和我的 main()。

 package com.cooksys.assessment;

 import javax.swing.AbstractListModel;
 import javax.swing.DefaultListModel;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JList;
 import javax.swing.JMenu;
 import javax.swing.JMenuBar;
 import javax.swing.JMenuItem;
 import javax.swing.JPanel;
 import javax.swing.plaf.ListUI;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;

public class Window {

private JFrame frame;

/**
 * Launch the application. The main method is the entry point to a Java
 * application. For this assessment, you shouldn't have to add anything to
 * this.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Window window = new Window();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
 }

/**
 * Create the application. This is the constructor for this Window class.
 * All of the code here will be executed as soon as a Window object is made.
 */

public Window() {
    initialize();
}

public void save() throws Exception {
    try {
    JAXBContext context = JAXBContext.newInstance(Configuration.class);
    Marshaller marshaller = context.createMarshaller();
    Configuration config = new Configuration();
    File file = new File("order.xml");
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(config, System.out);
    marshaller.marshal(config, file);
 }
    catch (JAXBException e) {
        e.printStackTrace();
    }
 }


/**
 * Initialize the contents of the frame. This is where Window Builder will
 * generate its code.
 */

@SuppressWarnings({ })
public void initialize() {
    frame = new JFrame();
    frame.getContentPane().setBackground(SystemColor.controlHighlight);
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);

    JMenu mnFile = new JMenu("File");
    menuBar.add(mnFile);

    JMenuItem mntmLoad = new JMenuItem("Load");
    mntmLoad.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    mnFile.add(mntmLoad);


    JMenuItem mntmSave = new JMenuItem("Save");
    mntmSave.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                save();
            }
            catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    });
    mnFile.add(mntmSave);

    JMenuItem mntmExit = new JMenuItem("Exit");
    mntmExit.setToolTipText("Exit application");
    mntmExit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }

    });
    mnFile.add(mntmExit);
    frame.getContentPane().setLayout(null);

    JPanel leftPanel = new JPanel();
    leftPanel.setBackground(Color.WHITE);
    leftPanel.setBounds(0, 0, 156, 240);
    frame.getContentPane().add(leftPanel);


    final DefaultListModel<Object> leftModel = new DefaultListModel<Object>();
    leftModel.addElement("Case");
    leftModel.addElement("Motherboard");
    leftModel.addElement("CPU");
    leftModel.addElement("GPU");
    leftModel.addElement("PSU");
    leftModel.addElement("RAM");
    leftModel.addElement("HDD");

    final JList<Object> leftList = new JList<Object>(leftModel);
    leftPanel.add(leftList);

    JPanel rightPanel = new JPanel();
    rightPanel.setBackground(Color.WHITE);
    rightPanel.setBounds(272, 0, 162, 240);
    frame.getContentPane().add(rightPanel);

    final DefaultListModel<Object> rightModel = new DefaultListModel<Object>();
    final JList<Object> config = new JList<Object>(rightModel);
    rightPanel.add(config);

    JButton addButton = new JButton(">>");
    addButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            for(Object selectedValue:leftList.getSelectedValuesList()) {
                rightModel.addElement(selectedValue);
                leftModel.removeElement(selectedValue);
            }
        }
    });
    addButton.setBounds(183, 58, 59, 23);
    frame.getContentPane().add(addButton);

    JButton removeButton = new JButton("<<");       
    removeButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            for(Object selectedValue:config.getSelectedValuesList()) {
                leftModel.addElement(selectedValue);
                rightModel.removeElement(selectedValue);
            }
        }
    });
    removeButton.setBounds(183, 123, 59, 23);
    frame.getContentPane().add(removeButton);



}
}

这是我的 Configuration.java 文件的内容。任何帮助将不胜感激。

package com.cooksys.assessment;

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(name = "rightList")
@XmlRootElement(name = "Configuration")
public class Configuration {

protected List<String> rightList;

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

public List<String> getRightList() {
    return rightList;
}

public void setRightList(List<String> rightList) {
    this.rightList = rightList;
}
}

【问题讨论】:

  • 究竟是什么不工作?空的文件?数据错误?向潜在的回答者提供尽可能多的信息非常重要,以提高您快速获得高质量回答的机会。
  • XML 文件未创建,输出为空。运行应用程序时没有错误。

标签: java xml marshalling


【解决方案1】:

xml 编组很好,但配置始终为空。将 rightModel 的定义移到 save() 之前,以便 save 函数可以访问它。将 rightModel 中的所有元素添加到列表中,并将 config.rightList 设置为该列表:

final DefaultListModel<Object> rightModel = new DefaultListModel<Object>();

public void save() throws Exception {
    try {
        JAXBContext context = JAXBContext.newInstance(Configuration.class);
        Marshaller marshaller = context.createMarshaller();
        Configuration config = new Configuration();
        List<String> l = new ArrayList<>();
        Enumeration<Object> e = rightModel.elements();
        while (e.hasMoreElements()) {
            l.add(e.nextElement().toString());
        }
        config.setRightList(l);
        File file = new File("order.xml");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(config, System.out);
        marshaller.marshal(config, file);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多