将数组从JAVA编组到XML

时间:2015-08-27 21:49:39

标签: java xml marshalling

我有一个可供选择的个人电脑零件清单,并移到右侧创建清单。在文件菜单中,我需要能够保存,它应该封送rightList的内容并将其保存为XML文件。我已经尝试了我能想到的一切,但没有任何效果。请帮忙。

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

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

1 个答案:

答案 0 :(得分: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();
    }
}