Java 如何使用XStream将ArrayList序列化为XML?
我试图使用XStream将自定义对象的ArrayList序列化为XML,但在输出时得到一些奇怪的结果 这是我用于转换的代码Java 如何使用XStream将ArrayList序列化为XML?,java,xstream,Java,Xstream,我试图使用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 = xs
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;
最后是我在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>
0
0
564
0
200
18
582
100
300
假的
1024
0
812
0
400
0
812
0
400
假的
600
400
请记住,输出XML文件惊人地有64MB,所以我只包含了几行 正如您所说,您似乎正在序列化整个画布 我已经用下面的代码做了一些测试:
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);
}
}
包xStream;
导入java.awt.Shape;
导入java.awt.geom.Arc2D;
导入java.io.PrintWriter;
导入java.util.ArrayList;
导入com.thoughtworks.xstream.*;
导入com.thoughtworks.xstream.io.xml.DomDriver;
公共类应用程序{
/*画布上绘制的所有形状的列表*/
私有静态ArrayList形状=新ArrayList();
公共静态无效保存(字符串文件名){
/*1.初始化序列化程序*/
XStream XStream=newxstream(newdomdriver());
/*2.生成XML字符串*/
字符串xml=xstream.toXML(形状);
/*3.将XML字符串打印到具有给定文件名的文件中*/
试一试{
PrintWriter=新的PrintWriter(文件名);
write.write(xml);
writer.close();
}捕获(例外情况除外){
例如getMessage();
}
}
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
添加(新的Arc2D.Double(100100,10,10,0,90,Arc2D.CHORD));
添加(新的Arc2D.Double(100100,10,10,0180,Arc2D.CHORD));
application.save(“drawing.xml”);
系统出口(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>
1.
90
10
0
10
100
100
1.
1.
180
10
0
10
100
100
1.
我认为这很好
David你说你得到了“奇怪的输出结果”。您期望得到什么?我期望得到类似于此结果的XML的东西我的输出感觉就像XStream正在序列化整个画布!是的,您正在序列化整个画布。我将检查application.getShapes()方法的代码。我看不出该方法有任何错误。但是如果你愿意自己检查的话,这里有一个问题。在publicsvoidsetstate(arraylistlist)中,你保存了什么?形状列表?还是画布的完整状态?
<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>