سؤال

I've created a wrapper class like below (is this necessary? I tried serializing the treemap directly without making any wrapper class and it failed) following the answer provided in a similar thread.

@Root
public class Example {

   @ElementMap(entry="property", value="value", attribute=true, inline=true)
   private TreeMap<String, String> map;

    public Example()
    {
        map = new TreeMap<String, String>() {
            {
                put("testing1", "a");
                put("testing2", "b");
                put("testing3", "c");
            }
        };
    }
}

Then I try to serialize it with the code below:

Serializer serializer = new Persister();
Example example = new Example();
File result = new File("example.xml");
serializer.write(example, result);

The example.xml file is generated, but it is an empty file. Is it because Treemap is not supported by Simple XML? Am I doing something wrong?

هل كانت مفيدة؟

المحلول

As per my comment, I've tried your code and it ran with no problems (on a stand-alone java app). So, my thoughts on what was happening would be:

  • Any compilation or classpath issue, which would probably be corrected on project cleanup
  • If you are debugging it on a physical phone, something may have happened on which it didn't let the program write on that file
  • Your app may had any problem with permissions on writing that file

Anyways, here's the generated file:

<example>
   <property string="testing1" value="a"/>
   <property string="testing2" value="b"/>
   <property string="testing3" value="c"/>
</example>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top