序列化objetcs仅打印最后一个

时间:2018-11-12 19:42:07

标签: java xml object serialization xml-serialization

当前我正面临这个问题:

我有一个对象数组(形状),当我尝试对其进行序列化时,它仅返回最后一个对象。

这里是用于保存整个项目以及形状数组列表的按钮。

//Setting action listener from the "save" button
    save.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                FileOutputStream out = null;
                PrintWriter print = null;
                String fName;
                JFileChooser jfc1 = new JFileChooser();
                jfc1.setAcceptAllFileFilterUsed(false);
                jfc1.setFileFilter(xmlfilter);
                jfc1.setDialogTitle("Enter the file's name to save");
                int value = jfc1.showSaveDialog((JMenuItem)e.getSource());
                if(value == JFileChooser.APPROVE_OPTION){
                    for(int i=0; i<images.size(); i++){
                        try{
                            fName = jfc1.getSelectedFile().getAbsolutePath();
                            if(!fName.endsWith(".xml")){
                                out = new FileOutputStream(fName + ".xml");
                                print = new PrintWriter(out);
                            }
                            else{
                                out = new FileOutputStream(fName);
                                print = new PrintWriter(out);
                            }
                            XStream xstream = new XStream(new DomDriver());
                            xstream.autodetectAnnotations(true);
                            String xml = xstream.toXML(images.get(i));
                            String auxTitle = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; 
                            xml = xml.substring(xml.indexOf("</javax.swing.JPanel>"));
                            xml = xml.replace("</javax.swing.JPanel>", "");
                            xml = xml.replace("</classes.Circle>", "");
                            xml = xml.replace("</classes.Rectangle>", "");
                            xml = xml.replace("</classes.Line>", "");
                            auxTitle = auxTitle + xml;
                            System.out.println(auxTitle);
                            print.println(auxTitle);
                            print.flush();
                        }
                        catch(IOException ex){
                            JOptionPane.showMessageDialog(null, "Error creating the file! Please, try again!");
                        }
                        finally{
                            if(out != null){
                                try{
                                    out.close();
                                }
                                catch(IOException exc){
                                    JOptionPane.showMessageDialog(null, "Error creating the file! Please, try again!");
                                }
                            }
                            else if(print != null){
                                    print.close();
                            }
                        }                       
                    }
                }
            }                   
    });

例如,我绘制了3个形状(一个圆,一个矩形和一条线),控制台显示了以下输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<classes.Shape>
<default>
  <height>104</height>
  <id>0</id>
  <idConnectedShape>0</idConnectedShape>
  <numClick>0</numClick>
  <width>122</width>
  <begin>
    <x>114</x>
    <y>87</y>
  </begin>
  <color>
    <red>0</red>
    <green>0</green>
    <blue>0</blue>
    <alpha>255</alpha>
  </color>
  <end>
    <x>236</x>
    <y>191</y>
  </end>
  <entries>
    <string>C:\\Users\\...\\Ferrari.jpg</string>
  </entries>
  <operator>ReadImage.</operator>
  <output>&apos;img.mat&apos;</output>
  <shape>Circle</shape>
</default>
</classes.Shape>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<classes.Shape>
<default>
  <height>20</height>
  <id>1</id>
  <idConnectedShape>0</idConnectedShape>
  <numClick>0</numClick>
  <width>20</width>
  <begin>
    <x>75</x>
    <y>139</y>
  </begin>
  <color>
    <red>0</red>
    <green>0</green>
    <blue>0</blue>
    <alpha>255</alpha>
  </color>
  <end>
    <x>95</x>
    <y>159</y>
  </end>
  <entries/>
  <shape>Rectangle</shape>
</default>
</classes.Shape>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<classes.Shape>
<default>
  <height>10</height>
  <id>2</id>
  <idConnectedShape>0</idConnectedShape>
  <numClick>0</numClick>
  <width>90</width>
  <begin>
    <x>85</x>
    <y>149</y>
  </begin>
  <color>
    <red>255</red>
    <green>0</green>
    <blue>0</blue>
    <alpha>255</alpha>
  </color>
  <end>
    <x>175</x>
    <y>139</y>
  </end>
  <entries/>
  <shape>Line</shape>
</default>
 </classes.Shape>

创建的文件为:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<classes.Shape>
<default>
  <height>10</height>
  <id>2</id>
  <idConnectedShape>0</idConnectedShape>
  <numClick>0</numClick>
  <width>90</width>
  <begin>
    <x>85</x>
    <y>149</y>
  </begin>
  <color>
    <red>255</red>
    <green>0</green>
    <blue>0</blue>
    <alpha>255</alpha>
  </color>
  <end>
    <x>175</x>
    <y>139</y>
  </end>
  <entries/>
  <shape>Line</shape>
</default>
</classes.Shape>

我想强调的是,已经尝试使用另一个XML序列化API,例如Xstream(我现在正在使用),JAXB,简单XML序列化,java.beans.XMLDecoder。不幸的是,一切都失败了。

1 个答案:

答案 0 :(得分:1)

问题是,您在循环中创建FileOutputStream和PrintWriter。 尝试类似

...
                   try{
                        fName = jfc1.getSelectedFile().getAbsolutePath();
                        if(!fName.endsWith(".xml")){
                            out = new FileOutputStream(fName + ".xml");
                            print = new PrintWriter(out);
                        }
                        else{
                            out = new FileOutputStream(fName);
                            print = new PrintWriter(out);
                        }
                        for(int i=0; i<images.size(); i++){
                          XStream xstream = new XStream(new DomDriver());
                          xstream.autodetectAnnotations(true);
                          String xml = xstream.toXML(images.get(i));
                          String auxTitle = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; 
                          xml = xml.substring(xml.indexOf("</javax.swing.JPanel>"));
                          xml = xml.replace("</javax.swing.JPanel>", "");
                          xml = xml.replace("</classes.Circle>", "");
                          xml = xml.replace("</classes.Rectangle>", "");
                          xml = xml.replace("</classes.Line>", "");
                          auxTitle = auxTitle + xml;
                          System.out.println(auxTitle);
                          print.println(auxTitle);
                          print.flush();
                        }
                    }
                    catch(IOException ex){
                        JOptionPane.showMessageDialog(null, "Error creating the file! Please, try again!");
                    }
                    finally{
                        if(out != null){
                            try{
                                out.close();
                            }
                            catch(IOException exc){
                                JOptionPane.showMessageDialog(null, "Error creating the file! Please, try again!");
                            }
                        }
                        else if(print != null){
                                print.close();
                        }
                    }
...