gpt4 book ai didi

java - 在 Java 中将对象序列化为 XML 会忽略我的对象的一些变量字段

转载 作者:数据小太阳 更新时间:2023-10-29 02:24:02 25 4
gpt4 key购买 nike

我试图将我的对象序列化为一个 xml 文件,问题是这个对象的一些实例变量没有写在 xml 文件中。我不知道为什么它们会被忽略,也不知道如何获取序列化方法来将每个变量写入 xml 文件中。我注意到,当我设置(使用 setter )时,其中一些变量更有可能写入 xml 文件。

我要问的是:

1- 我想知道为什么有些实例变量在对象被序列化时没有写在 XML 文件中。

2- 当我将该对象序列化为 XML 时,如何强制将该对象的所有实例变量写入文件。


我的类(class)

package DBMS;

import JDBC.ResultSetMetaData;

public class Column {

private String colName= "";
private String type="";
private boolean isPrimary= false;
private boolean isAutoIncrement= false;
private int NullableState = ResultSetMetaData.columnNullableUnknown;
private boolean isReadOnly= false;
private boolean isSearchable= true;
private String tableName= "unknown";
private int incrementNextValue = 1;


public Column() {
// TODO Auto-generated constructor stub

}

public Column(String n, String t) {

colName = n;
type=t;
}


public String getTableName() {
return tableName;
}

public void setTableName(String s) {
tableName = s;
}

public String getColName() {
// TODO Auto-generated method stub
return colName;
}

public void setColName(String s) {
// TODO Auto-generated method stub
colName = s;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public boolean isPrimary() {
return isPrimary;
}

public void setPrimary(boolean isPrimary) {
this.isPrimary = isPrimary;
}

public boolean isAutoIncrement() {
return isAutoIncrement;
}

public void setAutoIncrement(boolean isAutoIncrement) {
this.isAutoIncrement = isAutoIncrement;
}

public int getNullableState() {
return NullableState;
}

public void setNullable(int Nullable) {
this.NullableState = Nullable;
}

public boolean isReadOnly() {
return isReadOnly;
}

public void setReadOnly(boolean isReadOnly) {
this.isReadOnly = isReadOnly;
}

public boolean isSearchable() {
return isSearchable;
}

public void setSearchable(boolean isSearchable) {
this.isSearchable = isSearchable;
}

public int getIncrementNextValue() {
return incrementNextValue;
}

public void setIncrementNextValue(int incrementNextValue) {
this.incrementNextValue = incrementNextValue;
}

}

方法序列化

public  void serializeObjectToXML(String xmlFileLocation, ArrayList<Column> objectToSerialize) throws Exception {

FileOutputStream os = new FileOutputStream(xmlFileLocation);
XMLEncoder encoder = new XMLEncoder(os);
encoder.writeObject(objectToSerialize);
encoder.close();
os.close();
}

示例 xml 输出

 <?xml version="1.0" encoding="UTF-8"?>
-<java class="java.beans.XMLDecoder" version="1.7.0_07">
-<object class="java.util.ArrayList">
-<void method="add">
-<object class="DBMS.Column">
-<void property="autoIncrement"> <boolean>true</boolean> </void>
-<void property="colName"> <string>ID</string> </void>
-<void property="readOnly"> <boolean>true</boolean> </void>
-<void property="tableName"> <string>testSerialize</string> </void>
-<void property="type"> <string>int</string> </void> </object> </void>
-<void method="add">
-<object class="DBMS.Column">
-<void property="colName"> <string>Name</string> </void>
-<void property="primary"> <boolean>true</boolean> </void>
-<void property="tableName"> <string>testSerialize</string> </void>
-<void property="type"> <string>string</string> </void> </object> </void> -<void method="add">
-<object class="DBMS.Column">
-<void property="colName"> <string>info</string> </void>
-<void property="tableName"> <string>testSerialize</string> </void> -<void property="type"> <string>string</string> </void> </object> </void>
-<void method="add">
-<object class="DBMS.Column">
-<void property="colName"> <string>data</string> </void>
-<void property="tableName"> <string>testSerialize</string> </void>
-<void property="type"> <string>string</string> </void> </object> </void> </object> </java>

最佳答案

有两点值得注意:

  1. XMLEncoder 不查看私有(private)字段。它只查看匹配的“get”和“set”方法对(或“is”和“set”方法)。您有一个 getNullableState 方法,但设置方法不匹配;您应该将 setNullable 重命名为 setNullableState
  2. XMLEncoder 只为与其初始状态不同的属性写入值。每个属性的初始状态是对象最初构造时它具有的任何值。

因此,强制写入属性值的一种方法是确保它的值与构造对象时的值不同。

如果您想始终为每个属性编写 XML,您可能需要考虑使用 JAXB而不是 XMLEncoder。

关于java - 在 Java 中将对象序列化为 XML 会忽略我的对象的一些变量字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20306992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com