2011-06-01 61 views
1

比方说,下面的XML给出:反序列化重复的简单2.5.3(Java)的XML元素

<?xml version="1.0" encoding="UTF-8"?> 
<ResC> 
    <Err text="Error text 1"/> 
    <ConRes> 
     <Err text="Error text 2"/> 
     <ConList> 
      <Err text="Error text 3"/> 
      <Con> 
       <Err text="Error text 4"/> 
      </Con> 
     </ConList> 
    </ConRes> 
</ResC> 

正如你可以看到<Err>元素可以出现在XML的各个层面。使用Simple我想反序列化这个XML。所以,我创建了以下类:

@Element(required=false) 
public class Err { 
    @Attribute 
    private String text; 

    public void setText(String text) { this.text = text; } 

    public String getText() { return text; } 
} 

不过了,我怎么有注释类<ResC><ConRes><ConList><Con>?我是否真的必须在每个可能出现的类中声明<Err>类型的属性?这似乎是一个很大的开销。如果是这样,那么如果它包含错误,我将不得不检查每个对象。

有没有更好更简单的方法? :-)

感谢,
罗伯特

+0

优秀的问题btw。 – 2011-06-12 13:19:45

回答

0

要记住的重要一点是,简单的XML应该能够跟随你可以在逻辑上生成使用类的任何结构。所以你可以创建一个BaseClass,它使用一个错误接口并应用Decorator模式,以便它将所有这些传递给一个具体的错误类,而不需要任何实现对象需要知道它们是什么。

这可能没有意义。怎么样我只是告诉你......好吧......我只是走了,实现正是我想和这里的结果(full code link):

主文件:

package com.massaiolir.simple.iface; 

import java.io.File; 

import org.simpleframework.xml.Serializer; 
import org.simpleframework.xml.core.Persister; 

public class Main { 
    public static void main(String[] args) throws Exception { 
     Serializer serial = new Persister(); 
     ResC resc = serial.read(ResC.class, new File("data/testdata.xml")); 

     System.out.println(" == Printing out all of the error text. == "); 
     System.out.println(resc.getErrorText()); 
     System.out.println(resc.conRes.getErrorText()); 
     System.out.println(resc.conRes.conList.getErrorText()); 
     for (Con con : resc.conRes.conList.cons) { 
      System.out.println(con.getErrorText()); 
     } 
     System.out.println(" == Finished printing out all of the error text. == "); 
    } 
} 

它只是运行简单并显示结果。

的BaseObject.java类:

package com.massaiolir.simple.iface; 

import org.simpleframework.xml.Element; 

public class BaseObject implements Error { 
    @Element(name = "Err", required = false, type = ConcreteError.class) 
    private Error err; 

    @Override 
    public String getErrorText() { 
     return err.getErrorText(); 
    } 

    @Override 
    public void setErrorText(String errorText) { 
     err.setErrorText(errorText); 
    } 
} 

这是类,如果它想 '犯错' 一切都应该延长。

的错误接口:

package com.massaiolir.simple.iface; 

public interface Error { 
    void setErrorText(String errorText); 

    String getErrorText(); 
} 

的ConcreteError类:

package com.massaiolir.simple.iface; 

import org.simpleframework.xml.Attribute; 

public class ConcreteError implements Error { 
    @Attribute 
    private String text; 

    @Override 
    public String getErrorText() { 
     return text; 
    } 

    @Override 
    public void setErrorText(String errorText) { 
     this.text = errorText; 
    } 

} 

实际的实现类是在这之后。你会看到它们很微不足道,因为真正的工作正在上面的课程中处理。

的精读课:

package com.massaiolir.simple.iface; 

public class Con extends BaseObject { 

} 

的ConList类:

package com.massaiolir.simple.iface; 

import java.util.ArrayList; 

import org.simpleframework.xml.ElementList; 

public class ConList extends BaseObject { 
    @ElementList(entry = "Con", inline = true) 
    public ArrayList<Con> cons; 
} 

的ConRes类:

package com.massaiolir.simple.iface; 

import org.simpleframework.xml.Element; 

public class ConRes extends BaseObject { 
    @Element(name = "ConList") 
    public ConList conList; 
} 

的RESC类:

package com.massaiolir.simple.iface; 

import org.simpleframework.xml.Element; 
import org.simpleframework.xml.Root; 

@Root 
public class ResC extends BaseObject { 
    @Element(name = "ConRes") 
    public ConRes conRes; 
} 

,这是所有有给它。很简单的权利。十分钟之内我就能够完成所有的工作。实际上,我花了我更长的时间来编写这个响应,而不是写下我给你的代码。如果您对我刚刚编写的代码不了解,请告诉我。我希望这可以帮助你理解你如何去做这样的事情。

+0

非常感谢这个代码示例。这正是我一直在寻找的:-) – 2011-06-15 06:44:51

+0

@Robert Strauch:没有问题,希望它也可以帮助其他人。 – 2011-06-15 09:31:00