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

时间:2023-02-07 15:48:45

I am trying to serialize my object to an xml file, the problem is that some of the instance variables of this object are not written in the xml file. I don't know why are they ignored and how to get the serialize method to write every variable in the xml file. I have noticed that when I set (using the setter) some of these variables are more likely to be written in xml file.

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

What I am asking:

我要问的是:

1- I want to know why some of the instance variables are not written inside the XML file when their Object is serialized.

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

2- How to enforce all instance variables of my object to be written into the file, when I serialize that object to XML.

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


my 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;
}

}

method serialize

方法序列化

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();
}

sample xml output

示例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>

2 个解决方案

#1


3  

Two things worth noting:

值得注意的两件事:

  1. XMLEncoder does not look at private fields. It only looks at matching pairs of 'get' and 'set' methods (or 'is' and 'set' methods). You have a getNullableState method but the set-method does not match; you should rename setNullable to setNullableState.
  2. XMLEncoder不查看私有字段。它只关注匹配的'get'和'set'方法对(或'is'和'set'方法)。你有一个getNullableState方法但set-method不匹配;您应该将setNullable重命名为setNullableState。
  3. XMLEncoder only writes values for properties which are different from their initial state. The initial state of each property is whatever value it has when the object is initially constructed.
  4. XMLEncoder仅为与其初始状态不同的属性写入值。每个属性的初始状态是最初构造对象时的任何值。

So, one way to force a property value to be written is to make sure it has a value that's different from what it had when the object was constructed.

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

If you want to always write XML for every property, you may want to consider using JAXB instead of XMLEncoder.

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

#2


2  

XMLEncoder uses the JavaBean pattern to serialize an object.

XMLEncoder使用JavaBean模式来序列化对象。

That means, it looks for a specific set of method names, not member variables, and calls those methods to transfer the resulting values to an XML document.

这意味着,它会查找一组特定的方法名称,而不是成员变量,并调用这些方法将结果值传输到XML文档。

#1


3  

Two things worth noting:

值得注意的两件事:

  1. XMLEncoder does not look at private fields. It only looks at matching pairs of 'get' and 'set' methods (or 'is' and 'set' methods). You have a getNullableState method but the set-method does not match; you should rename setNullable to setNullableState.
  2. XMLEncoder不查看私有字段。它只关注匹配的'get'和'set'方法对(或'is'和'set'方法)。你有一个getNullableState方法但set-method不匹配;您应该将setNullable重命名为setNullableState。
  3. XMLEncoder only writes values for properties which are different from their initial state. The initial state of each property is whatever value it has when the object is initially constructed.
  4. XMLEncoder仅为与其初始状态不同的属性写入值。每个属性的初始状态是最初构造对象时的任何值。

So, one way to force a property value to be written is to make sure it has a value that's different from what it had when the object was constructed.

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

If you want to always write XML for every property, you may want to consider using JAXB instead of XMLEncoder.

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

#2


2  

XMLEncoder uses the JavaBean pattern to serialize an object.

XMLEncoder使用JavaBean模式来序列化对象。

That means, it looks for a specific set of method names, not member variables, and calls those methods to transfer the resulting values to an XML document.

这意味着,它会查找一组特定的方法名称,而不是成员变量,并调用这些方法将结果值传输到XML文档。