Simple XML Framework : Having an “inline like” behaviour for objects in ElementMap

那年仲夏 提交于 2019-12-23 19:22:24

问题


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

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

I created an inner class that contains only the Hashmap that i'm interested in, as i was unable to serialize it the way it is (and read that it's not possible) added an object to test like this listEval.put(0,currentEvaluation). following, the inner class :

@Root (name="ROWSET")
public static class listOfEvals {

    @ElementMap (entry="ROW", key="num", attribute=true, inline=true)
    private Map<Integer, EvaluationContent> evalList;

    public listOfEvals(Map<Integer, EvaluationContent> list){
        evalList=list;
    }

    public Map<Integer, EvaluationContent> getEvalList() {
        return evalList;
    }

    public void setEvalList(Map<Integer, EvaluationContent> evalList) {
        this.evalList = evalList;
    }
}

EvaluationContent object is defined like this :

public class EvaluationContent {

    @Element(name="Name", required = false)
    private String mName; 
    @Element(name="FNAME", required = false)         
    private String mFname;
    @Element(name="BIRTH", required = false)        
    private String mBirth; 
    @Element(name="Num", required = false)        
    private String mNum; 

    public String getName() {
        return mName;
    }
    public void setName(String mName) {
        this.mName = mName;
    }
     ... 
    }

The problem is that i'm getting a <evaluationContent> tag for each entry:

<ROWSET>
       <ROW num="0">
          <evaluationContent>
            <Name>foo</Name>
            <FNAME>bar</FNAME>
            <BIRTH>01/01/2000</BIRTH>
            <Num>4376484</NUM>
          </evaluationContent>
       </ROW>
       <ROW num="1">
          <evaluationContent>
         ...
          <evaluationContent>
       </ROW>
    </ROWSET>

There must be a better way to achieve that but i'm unable to figure out how, thanks for your help


回答1:


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

Registry registry = new Registry();

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


    @Override
    public void write(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();
            final EvaluationContent content = entry.getValue();

            // Here's the ugly part: creating the full node
            final OutputNode child = 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());
        }   
    }
});

Strategy strategy = new RegistryStrategy(registry);
Serializer ser = new Persister(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>
   <ROW num="0">
      <Name>foo</Name>
      <FNAME>bar</FNAME>
      <BIRTH>01/01/2000</BIRTH>
      <Num>4376484</Num>
   </ROW>
   <ROW num="1">
      <Name>foo</Name>
      <FNAME>bar</FNAME>
      <BIRTH>02/02/2000</BIRTH>
      <Num>4376484</Num>
   </ROW>
</ROWSET>

Documentation:

  • Tutorials / Examples (look for Converter there)
  • API Documentation preliminary packages convert, transform and strategy


来源:https://stackoverflow.com/questions/17171504/simple-xml-framework-having-an-inline-like-behaviour-for-objects-in-elementm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!