В настоящее время я сталкиваюсь с этой проблемой:
У меня есть массив объектов (Shapes), и когда я пытаюсь его сериализовать, он возвращает мне только последний.
Вот кнопка, которая сохраняет весь проект вместе с массивом фигур.
//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>'img.mat'</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>
Я хотел бы подчеркнуть, что уже пытался использовать другие API-интерфейсы XML-сериализации, такие как Xstream (я использую сейчас), JAXB, Simple XML Serialization, java.beans.XMLDecoder. К сожалению, все не удалось.