Skip to content Skip to sidebar Skip to footer

Simple Xml Framework : Having An "inline Like" Behaviour For Objects In Elementmap

i am trying to serialize an Hashmap of custom Objects on Android to get an xml like : fooba

Solution 1:

I have a solution - but not it's not perfect:

Registryregistry=newRegistry();

// Bind the list's class to it's converter. You also can implement it as a "normal" class.
registry.bind(EvaluationContent.ListOfEvals.class, newConverter<EvaluationContent.ListOfEvals>()
{
    @Overridepublic EvaluationContent.ListOfEvals read(InputNode node)throws Exception
    {
        /* Implement if required */thrownewUnsupportedOperationException("Not supported yet.");
    }


    @Overridepublicvoidwrite(OutputNode node, EvaluationContent.ListOfEvals value)throws Exception
    {
        Iterator<Map.Entry<Integer, EvaluationContent>> itr = value.getEvalList().entrySet().iterator();

        while( itr.hasNext() )
        {
            final Entry<Integer, EvaluationContent> entry = itr.next();
            finalEvaluationContentcontent= entry.getValue();

            // Here's the ugly part: creating the full nodefinalOutputNodechild= node.getChild("ROW");

            child.setAttribute("num", entry.getKey().toString());
            child.getChild("Name").setValue(content.getName());
            child.getChild("FNAME").setValue(content.getFName());
            child.getChild("BIRTH").setValue(content.getBirth());
            child.getChild("Num").setValue(content.getNum());
        }   
    }
});

Strategystrategy=newRegistryStrategy(registry);
Serializerser=newPersister(strategy);
ser.write(list, f); // f is the Output (eg. a file) where you write to

You can set the converter by using @Converter() attribute too. Here's how to do so:

  1. Write a class that implements Converter<EvaluationContent> interface, eg. EvalListConverter
  2. Set @Convert() Attribute to the list class, eg. @Convert(value = EvalListConverter.class)
  3. set AnnotationStrategy to persister: Serializer ser = new Persister(new AnnotationStrategy())

Another way is to implement a converter that uses a Serializer to write the nodes to list nodes. Hoewer, you really have to play around a bit.

For testing i've put the two values from your example into the list and serialized it, resulting Xml:

<ROWSET><ROWnum="0"><Name>foo</Name><FNAME>bar</FNAME><BIRTH>01/01/2000</BIRTH><Num>4376484</Num></ROW><ROWnum="1"><Name>foo</Name><FNAME>bar</FNAME><BIRTH>02/02/2000</BIRTH><Num>4376484</Num></ROW></ROWSET>

Documentation:

Post a Comment for "Simple Xml Framework : Having An "inline Like" Behaviour For Objects In Elementmap"