Re: [Simple-support] ElementMap without entry wrapper?
Brought to you by:
niallg
|
From: Niall G. <gal...@ya...> - 2011-11-24 10:13:33
|
Take a look at org.simpleframework.xml.util.Dictionary, it can do this.
________________________________
From: Jason von Nieda <ja...@vo...>
To: sim...@li...
Sent: Wednesday, 23 November 2011 10:53 PM
Subject: [Simple-support] ElementMap without entry wrapper?
Hi folks, I am evaluating Simple for use as the configuration framework for an application I am writing. I am currently using XStream but I prefer Simple's explicit mapping vs. XStream's implicit mapping. I am trying to figure out how to emit a Map that does not include the redundant <entry> and key information.
Here is my test program:
==
package org.openpnp.app;
import java.io.StringWriter;
import java.util.HashMap;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementMap;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
public class Test {
public static void main(String[] args) throws Exception {
Serializer serializer = new Persister();
StringWriter writer = new StringWriter();
serializer.write(new Machine(), writer);
System.out.println(writer.toString());
}
@Root
public static class Machine {
@ElementMap(attribute=true, key="id")
private HashMap<String, Head> heads = new HashMap<String, Head>();
public Machine() {
heads.put("1", new Head("1"));
heads.put("2", new Head("2"));
}
}
public static class Head {
@Attribute
private String id;
public Head(String id) {
this.id = id;
}
}
}
==
And the output:
==
<machine>
<heads>
<entry id="2">
<head id="2"/>
</entry>
<entry id="1">
<head id="1"/>
</entry>
</heads>
</machine>
==
And what I would prefer the output to look like:
==
<machine>
<heads>
<head id="2"/>
<head id="1"/>
</heads>
</machine>
==
I expect this to be possible because I am telling Simple that the key field is id. If it knows that, the entry element and it's id= attribute is redundant information. I am able to do this quite easily with XStream but I have not found a way to do it with Simple. Is it possible without writing my own Map converter?
Thanks,
Jason
------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure
contains a definitive record of customers, application performance,
security threats, fraudulent activity, and more. Splunk takes this
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
Simple-support mailing list
Sim...@li...
https://lists.sourceforge.net/lists/listinfo/simple-support |