使用jaxb将java对象转换为xml,反之亦然(封送和解封)

使用jaxb将java对象转换为xml,反之亦然(封送和解封),java,xml,swing,jaxb,marshalling,Java,Xml,Swing,Jaxb,Marshalling,我假设有一个名为save的方法,它应该将右侧面板中的计算机部件列表封送到一个XML文件中。相反,另一个名为load的方法应该将保存的XML文件解组回对象中 因此,基本上,Save事件将调用Save方法,并将右侧面板中的零件列表保存到XML文件中。加载事件应清除右侧面板,并调用加载方法 调用load时,它应该在右侧面板中显示未分组的数据。我得出去工作了 不过,我很难计算出负载和节省零件 import javax.swing.*; import java.awt.*; import java.awt

我假设有一个名为save的方法,它应该将右侧面板中的计算机部件列表封送到一个XML文件中。相反,另一个名为load的方法应该将保存的XML文件解组回对象中

因此,基本上,Save事件将调用Save方法,并将右侧面板中的零件列表保存到XML文件中。加载事件应清除右侧面板,并调用加载方法

调用load时,它应该在右侧面板中显示未分组的数据。我得出去工作了

不过,我很难计算出负载和节省零件

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class PCParts implements ActionListener{

    JList destinationList, sourceList;
    JButton buttonin, buttonout;

    DefaultListModel source, destination;

    public JPanel createContentPane (){

        JPanel totalGUI = new JPanel();

        source = new DefaultListModel();
        destination = new DefaultListModel();

        String shoppingItems[] = {"Case", "Motherboard", "CPU", "RAM", "GPU",
        "HDD", "PSU"};

        for(int i = 0; i < shoppingItems.length; i++)
        {
            source.addElement(shoppingItems[i]);
        }

        destinationList = new JList(source);
        destinationList.setVisibleRowCount(10);
        destinationList.setFixedCellHeight(20);
        destinationList.setFixedCellWidth(140);
        destinationList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        JScrollPane list1 = new JScrollPane(destinationList);

        sourceList = new JList(destination);
        sourceList.setVisibleRowCount(10);
        sourceList.setFixedCellHeight(20);
        sourceList.setFixedCellWidth(140);
        sourceList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        JScrollPane list2 = new JScrollPane(sourceList);

        JPanel buttonPanel = new JPanel();

        buttonin = new JButton(">>");
        buttonin.setHorizontalAlignment(SwingConstants.RIGHT);
        buttonin.addActionListener(this);
        buttonPanel.add(buttonin);

        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));

        bottomPanel.add(Box.createRigidArea(new Dimension(10,0)));
        bottomPanel.add(list1);
        bottomPanel.add(Box.createRigidArea(new Dimension(5,0)));
        bottomPanel.add(buttonPanel);

                buttonout = new JButton("<<");
                buttonout.addActionListener(this);
                buttonPanel.add(buttonout);
        bottomPanel.add(Box.createRigidArea(new Dimension(5,0)));
        bottomPanel.add(list2);
        bottomPanel.add(Box.createRigidArea(new Dimension(10,0)));

        totalGUI.add(bottomPanel);
        totalGUI.setOpaque(true);
        return totalGUI;
    }

    private JPanel createSquareJPanel(Color color, int size) {
        JPanel tempPanel = new JPanel();
        tempPanel.setBackground(color);
        tempPanel.setMinimumSize(new Dimension(size, size));
        tempPanel.setMaximumSize(new Dimension(size, size));
        tempPanel.setPreferredSize(new Dimension(size, size));
        return tempPanel;
    }

    public void actionPerformed(ActionEvent e) 
    {
        int i = 0;


        if(e.getSource() == buttonin)
        {
            int[] fromindex = destinationList.getSelectedIndices();
            Object[] from = destinationList.getSelectedValues();

            for(i = 0; i < from.length; i++)
            {
                destination.addElement(from[i]);
            }

            for(i = (fromindex.length-1); i >=0; i--)
            {
                source.remove(fromindex[i]);
            }
        }

        else if(e.getSource() == buttonout)
        {
            Object[] to = sourceList.getSelectedValues();
            int[] toindex = sourceList.getSelectedIndices();

            for(i = 0; i < to.length; i++)
            {
                source.addElement(to[i]);
            }

            for(i = (toindex.length-1); i >=0; i--)
            {
                destination.remove(toindex[i]);
            }
        }
    }



    private static void createAndShowGUI() {

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("PC Parts Builder");
        JMenu file = new JMenu ("File");
        file.setMnemonic (KeyEvent.VK_F);

        PCParts demo = new PCParts();
        frame.setContentPane(demo.createContentPane());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

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

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

        JMenuItem item;
        file.add(item = new JMenuItem("Load"));
        item.setMnemonic (KeyEvent.VK_O);
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                doOpenCommand();
            }

            private void doOpenCommand() {
                // TODO Auto-generated method stub

            }


        });

        mnFile.add(item);


        JMenuItem mntmSave = new JMenuItem("Save");
        mntmSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                doSaveCommand();

            }

            private void doSaveCommand() {

            }
        });
        mnFile.add(mntmSave);

        JMenuItem mntmNewMenuItem = new JMenuItem("Exit");
        mntmNewMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        mnFile.add(mntmNewMenuItem);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
只需创建一个类来保存一个列表。然后,您可以只封送/取消封送到该类的实例

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Parts {

    @XmlElement(name = "part")
    private List<String> part;

    public List<String> getPart() { return part; }
    public void setPart(List<String> part) { this.part = part; }
}
注意,您有一个小的设计问题。无法访问需要访问的DefaultListModels,因为侦听器代码位于静态上下文中,并且模型不是静态的。我只是将模型设置为静态以使其正常工作,但您需要重新设计代码。这是标准输出的结果-您将需要封送到文件

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<parts>
    <part>Case</part>
    <part>Motherboard</part>
    <part>CPU</part>
</parts>
我会让你自己去做解密工作。这应该让你开始

一些资源


非常感谢您的回复,它在编组方面对我帮助很大。不幸的是,我仍在为解包部分而挣扎。这是我为Load函数编写的代码,但它不会加载XML文件中的内容。我做错了什么?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<parts>
    <part>Case</part>
    <part>Motherboard</part>
    <part>CPU</part>
</parts>