Re: [Simple-support] Writing to XML fails
Brought to you by:
niallg
|
From: Miha V. <mih...@gm...> - 2014-06-12 15:56:50
|
Hi,
could you please help me get this working? I really do not know what is
wrong. I haven't had the time to look into the simple sources yet...
Thanks, Miha
On 7.6.2014 10:16, Miha Vitorovic wrote:
> Thanks, I had a similar idea over the night, but I cannot get it working:
>
> package com.mycompany.simpletest;
>
> import org.simpleframework.xml.Element;
> import org.simpleframework.xml.Root;
>
> @Root
> public class Pair<A, B> {
> @Element(name = "first")
> private final A first;
> @Element(name = "second")
> private final B second;
>
> public Pair(@Element(name = "first") A first, @Element(name =
> "second") B second) {
> this.first = first;
> this.second = second;
> }
>
> public A getFirst() { return first; }
> public B getSecond() { return second; }
> }
> - - - - - - - -
> package com.mycompany.simpletest;
>
> import java.util.ArrayList;
> import java.util.List;
> import org.simpleframework.xml.Element;
> import org.simpleframework.xml.ElementList;
> import org.simpleframework.xml.Root;
>
> @Root
> public class Test {
> @Root
> public class Info {
> @Element(name = "name")
> private final String name;
>
> public Info(@Element(name = "name") String name) { this.name =
> name; }
>
> public String getName() { return name; }
> }
>
> @ElementList
> private List<Pair<Info, Info>> list;
>
> public Test() {
> list = new ArrayList<>();
> }
>
> public void add(String s1, String s2) {
> Info i1 = new Info(s1);
> Info i2 = new Info(s2);
> Pair<Info, Info> p = new Pair<>(i1, i2);
> list.add(p);
> }
> }
> - - - - - - - -
> package com.mycompany.simpletest;
>
> import java.io.FileOutputStream;
> import org.simpleframework.xml.Serializer;
> import org.simpleframework.xml.convert.AnnotationStrategy;
> import org.simpleframework.xml.core.Persister;
> import org.simpleframework.xml.strategy.Strategy;
>
> public class SimpleTest {
>
> public static void main(String[] args) {
> Test t1 = new Test();
> t1.add("a", "b");
> t1.add("b", "c");
>
> try {
> FileOutputStream out = new FileOutputStream("test.xml");
> Strategy strategy = new AnnotationStrategy();
> Serializer serializer = new Persister(strategy);
> serializer.write(t1, out);
> out.close();
> } catch (Exception e) { e.printStackTrace(); }
> }
> }
>
> Br, Miha
>
> On 7.6.2014 3:58, Niall Gallagher wrote:
>> You cannot construct a non static inner class through reflection. For the constructors also you must specify the names
>>
>> Pair(@Element(name="first") A first, @Element(name="second") B second)
>>
>> --------------------------------------------
>> On Fri, 6/6/14, Miha Vitorovic<mih...@gm...> wrote:
>>
>>
>
|