Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 简单的XML框架:有一个;内联式;ElementMap中对象的行为_Java_Android_Xml_Xml Serialization_Simple Framework - Fatal编程技术网

Java 简单的XML框架:有一个;内联式;ElementMap中对象的行为

Java 简单的XML框架:有一个;内联式;ElementMap中对象的行为,java,android,xml,xml-serialization,simple-framework,Java,Android,Xml,Xml Serialization,Simple Framework,我正在尝试序列化Android上自定义对象的Hashmap,以获得如下xml: <ROWSET> <ROW num="0"> <Name>foo</Name> <FNAME>bar</FNAME> <BIRTH>01/01/2000</BIRTH> <Num>4376484</NUM> </ROW>

我正在尝试序列化Android上自定义对象的Hashmap,以获得如下xml:

<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>
EvaluationContent对象的定义如下:

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;
    }
     ... 
    }
问题是,每个条目都有一个
标记:

<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>

福
酒吧
01/01/2000
4376484
...

必须有更好的方法来实现这一点,但我不知道如何实现,感谢您的帮助

我有一个解决方案-但它并不完美:

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
文档:

  • /(在那里查找转换器)
  • 初步包
    转换
    转换
    策略

感谢您的贡献@ollo,但是我的实际类大约有100个元素,因此此解决方案的“不好部分”将被扩展,我知道这对于我的案例来说不是一个可行的解决方案,然后您可以使用序列化程序及其策略。可以在转换器中使用序列化程序,这可能会对您有所帮助。
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
<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>