我正在使用simple-xml在我的Java应用程序中执行XML序列化/反序列化。我有一个课程如下:
@Root(name="config")
public class Config{
@Element(name="update_interval")
private int updateInterval;
@Element(name="timestamp")
private long timestamp;
//...
//...
}
现在,这将产生如下所示的XML:
<config>
<update_interval>2000</update_interval>
<timestamp>1234567890</timestamp>
</config>
问题:
如何在运行时覆盖元素名称,以便在某些情况下,XML如下所示?
<config>
<updt_int>2000</updt_int>
<ts>1234567890</ts>
</config>
为了澄清,我想仅在某些情况下覆盖元素名称。所以基本上,
if(condition){
//Override Element Names
} else {
//Serialize Normally
}
答案 0 :(得分:1)
由于this comment,我发现在这种情况下实现序列化的简便方法。
但是,我无法 反序列化 这样的XML文档。这是我的部分解决方案:
/*
* Config.java
*/
@Root(name="config", strict = false)
public class Config {
@Element(name="timestamp", required = false)
private long timestamp;
@Element(name = "update_interval", required = false)
private int updateInterval;
public Config() {
}
public int getUpdateInterval() {
return updateInterval;
}
public void setUpdateInterval(int updateInterval) {
this.updateInterval = updateInterval;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
@Override
public String toString() {
return "Config{" +
"timestamp=" + timestamp +
", updateInterval=" + updateInterval +
'}';
}
}
/*
* Custom Visitor implementation
*/
public class MyInterceptor implements Visitor {
private static int sReadCount = 0;
private static int sWriteCount = 0;
@Override
public void read(Type field, NodeMap<InputNode> node) throws Exception {
/*
* This is where I need help!
*
*
* This method is only called once: for the <config> node
* It is not called for the other nodes since they are not "recognized"
* i.e., there are no annotations for the nodes <ts> and <updt_int>
*/
System.out.println("Read Count : "+ (++sReadCount));
System.out.println(node.getName());
System.out.println(node.getNode());
}
@Override
public void write(Type field, NodeMap<OutputNode> node) throws Exception {
/*
* This works like a charm.
*/
System.out.println("Write Count : "+ (++sWriteCount));
OutputNode opNode = node.getNode();
if("timestamp".equals(opNode.getName())){
opNode.setName("ts");
}
if("update_interval".equals(opNode.getName())){
opNode.setName("updt_int");
}
}
}
/*
*
*/ Main class
public class Bootstrap {
static final Random RANDOM = new Random();
public static void main(String [] args){
Config cfg = new Config();
cfg.setTimestamp(RANDOM.nextLong());
cfg.setUpdateInterval(1000);
Serializer serializer = new Persister(new VisitorStrategy(new MyInterceptor()));
StringWriter writer = new StringWriter();
try {
serializer.write(cfg, writer);
} catch (Exception e) {
e.printStackTrace();
}
String serialized = writer.toString();
System.out.println(serialized);
Config desCfg = null;
try {
desCfg = serializer.read(Config.class, serialized);
} catch (Exception e) {
e.printStackTrace();
}
if(desCfg != null){
System.out.println(desCfg.toString());
}
}
}