我正在尝试使用XStream将自定义对象的ArrayList序列化为XML,但是我得到了一些奇怪的输出结果。
以下是我用于转换的代码,
public void save (String fileName) {
/* 1. Initialize the serializer. */
XStream xstream = new XStream(new DomDriver());
/* 2. Generate the XML string. */
String xml = xstream.toXML(application.getShapes());
/* 3. Print the XML string into a file with the given file name. */
try {
PrintWriter writer = new PrintWriter(fileName);
writer.write(xml);
writer.close();
} catch (Exception ex) {
ex.getMessage();
}
}
这是我要序列化的ArrayList,
/* List of all the shapes drawn on the canvas. */
private ArrayList<Shape> shapes;
这是对save方法的调用,
public void saveClicked() {
application.setSaveNLoadstrategy(new XmlStrategy());
application.save("drawing.xml");
}
最后这是我在XML文件中得到的输出
<list>
<model.Shape>
<application>
<canvas>
<dirtyBits>0</dirtyBits>
<__geomBounds class="com.sun.javafx.geom.RectBounds">
<minX>0.0</minX>
<maxX>564.0</maxX>
<minY>0.0</minY>
<maxY>200.0</maxY>
</__geomBounds>
<__txBounds class="com.sun.javafx.geom.RectBounds">
<minX>18.0</minX>
<maxX>582.0</maxX>
<minY>100.0</minY>
<maxY>300.0</maxY>
</__txBounds>
<pendingUpdateBounds>false</pendingUpdateBounds>
<parent class="javafx.scene.Node$1">
<value class="javafx.scene.layout.Pane">
<dirtyBits>1024</dirtyBits>
<__geomBounds class="com.sun.javafx.geom.RectBounds">
<minX>0.0</minX>
<maxX>812.0</maxX>
<minY>0.0</minY>
<maxY>400.0</maxY>
</__geomBounds>
<__txBounds class="com.sun.javafx.geom.RectBounds">
<minX>0.0</minX>
<maxX>812.0</maxX>
<minY>0.0</minY>
<maxY>400.0</maxY>
</__txBounds>
<pendingUpdateBounds>false</pendingUpdateBounds>
<parentDisabledChangedListener class="null"/>
<parentTreeVisibleChangedListener class="null"/>
<scene>
<value class="javafx.scene.Scene">
<widthSetByUser>600.0</widthSetByUser>
<heightSetByUser>400.0</heightSetByUser>
请记住,输出XML文件令人惊讶的是64 MB,所以我只包括几行!
答案 0 :(得分:0)
正如你所说,似乎你正在序列化整个画布。
我已经使用以下代码完成了一些测试:
package xStream;
import java.awt.Shape;
import java.awt.geom.Arc2D;
import java.io.PrintWriter;
import java.util.ArrayList;
import com.thoughtworks.xstream.*;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class application {
/* List of all the shapes drawn on the canvas. */
private static ArrayList<Shape> shapes = new ArrayList<Shape>();
public static void save (String fileName) {
/* 1. Initialize the serializer. */
XStream xstream = new XStream(new DomDriver());
/* 2. Generate the XML string. */
String xml = xstream.toXML(shapes);
/* 3. Print the XML string into a file with the given file name. */
try {
PrintWriter writer = new PrintWriter(fileName);
writer.write(xml);
writer.close();
} catch (Exception ex) {
ex.getMessage();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
shapes.add(new Arc2D.Double(100,100,10,10,0,90, Arc2D.CHORD));
shapes.add(new Arc2D.Double(100,100,10,10,0,180, Arc2D.CHORD));
application.save("drawing.xml");
System.exit(0);
}
}
我得到了结果:
<list>
<java.awt.geom.Arc2D_-Double serialization="custom">
<unserializable-parents>
<type>1</type>
</unserializable-parents>
<java.awt.geom.Arc2D_-Double>
<default>
<extent>90.0</extent>
<height>10.0</height>
<start>0.0</start>
<width>10.0</width>
<x>100.0</x>
<y>100.0</y>
</default>
<byte>1</byte>
</java.awt.geom.Arc2D_-Double>
</java.awt.geom.Arc2D_-Double>
<java.awt.geom.Arc2D_-Double serialization="custom">
<unserializable-parents>
<type>1</type>
</unserializable-parents>
<java.awt.geom.Arc2D_-Double>
<default>
<extent>180.0</extent>
<height>10.0</height>
<start>0.0</start>
<width>10.0</width>
<x>100.0</x>
<y>100.0</y>
</default>
<byte>1</byte>
</java.awt.geom.Arc2D_-Double>
</java.awt.geom.Arc2D_-Double>
</list>
我觉得这很好。
大卫