Reputation: 31
currently I am facing this problem:
I have an arraylist of objects (Shapes) and when I try to serialize it, it returns me only the last one.
Here is the button that saves the whole project with the arraylist of 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();
}
}
}
}
}
});
For example, I drew 3 shapes (a circle, a rectangle and a line), and console showed me this output:
<?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>
And the file created is:
<?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>
I would like to emphasize that have already tried to use another XML-serialization API's, like Xstream(I am using now), JAXB, Simple XML Serialization, java.beans.XMLDecoder. Unfortunately, all have failed.
Upvotes: 0
Views: 31
Reputation: 640
Problem is, that you create FileOutputStream and PrintWriter in the loop. try something like
...
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();
}
}
...
Upvotes: 1