从java对象创建xml文件时出错
当我尝试将我的CreateNode对象存储在xml文件中时,会出现以下错误从java对象创建xml文件时出错,java,xml,serialization,xmlencoder,Java,Xml,Serialization,Xmlencoder,当我尝试将我的CreateNode对象存储在xml文件中时,会出现以下错误 java.lang.InstantiationException: GraphPanel$CreateNode Continuing ... java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(GraphPanel$CreateNode); Continuing ... 这是我的节点类 public static cl
java.lang.InstantiationException: GraphPanel$CreateNode
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(GraphPanel$CreateNode);
Continuing ...
这是我的节点类
public static class CreateNode {
private Point p;
private int r;
private String NAME;
private String nodeid;
private boolean selected = false;
private boolean fnode;
private Rectangle b = new Rectangle();
/**
* Construct a new node.
*/
public CreateNode(String nodeid, Point p, int r, String NAME, boolean fnode) {
this.nodeid = nodeid;
this.p = p;
this.r = r;
this.NAME = NAME;
setBoundary(b);
System.out.print(p.toString());
}
/**
* Calculate this node's rectangular boundary.
*/
private void setBoundary(Rectangle b) {
b.setBounds(p.x - r, p.y - r, 2 * r, 2 * r);
}
/**
* Select each node in r.
*/
public static void selectRect(List<CreateNode> list, Rectangle r) {
for (CreateNode n: list) {
n.setSelected(r.contains(n.p));
}
}
/**
* Toggle selected state of each node containing p.
*/
public static void selectToggle(List<CreateNode> list, Point p) {
for (CreateNode n: list) {
if (n.contains(p)) {
n.setSelected(!n.isSelected());
}
}
}
}
我试图在这里序列化/存储xml文件中的对象,但它给了我错误
private class Serialize extends AbstractAction {
public Serialize(String name) {
super(name);
}
public void actionPerformed(ActionEvent e) {
ListIterator<CreateNode> Fiter = nodes.listIterator();
while (Fiter.hasNext()) {
CreateNode n = Fiter.next();
FileOutputStream os;
try {
os = new FileOutputStream("D:/DM1/pert.xml");
XMLEncoder encoder = new XMLEncoder(os);
encoder.writeObject(n);
encoder.close();
}
catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
私有类序列化扩展了AbstractAction{
公共序列化(字符串名称){
超级(姓名);
}
已执行的公共无效操作(操作事件e){
ListIterator Fiter=nodes.ListIterator();
while(Fiter.hasNext()){
CreateNode n=Fiter.next();
FileOutputOS;
试一试{
os=新文件输出流(“D:/DM1/pert.xml”);
XMLEncoder编码器=新的XMLEncoder(os);
编码器。写入对象(n);
encoder.close();
}
捕获(FileNotFoundException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
}
}
}
我知道文件应该在外部创建,但因为我只是在玩代码,所以我也尝试使用默认构造函数,但只获取xml文件中的选定属性。
private class Serialize extends AbstractAction {
public Serialize(String name) {
super(name);
}
public void actionPerformed(ActionEvent e) {
ListIterator<CreateNode> Fiter = nodes.listIterator();
while (Fiter.hasNext()) {
CreateNode n = Fiter.next();
FileOutputStream os;
try {
os = new FileOutputStream("D:/DM1/pert.xml");
XMLEncoder encoder = new XMLEncoder(os);
encoder.writeObject(n);
encoder.close();
}
catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}