Java 避免在简单框架输出中使用空元素标记

Java 避免在简单框架输出中使用空元素标记,java,simple-framework,Java,Simple Framework,序列化所需元素时,正确的避免方法是什么 例如: @ElementList(name=“rpc”,required=true) 公共ArrayList getRequestedFunctions(){ 返回请求的函数; } 空列表将导致引用的“空元素标记”样式的单个XML元素: 我真正需要的是以下陈述: 编辑:我需要一个不同类型的列表解决方案!例如,可能还有列表,列表,列表。。。加上注释的不同名称属性,在示例中为“rpc” 谢谢你的帮助 解决 我将首先介绍我实际实现的解决方案,以及我在项

序列化所需元素时,正确的避免方法是什么

例如:

@ElementList(name=“rpc”,required=true)
公共ArrayList getRequestedFunctions(){
返回请求的函数;
}
空列表将导致引用的“空元素标记”样式的单个XML元素:


我真正需要的是以下陈述:


编辑:我需要一个不同类型的列表解决方案!例如,可能还有
列表
列表
列表
。。。加上注释的不同名称属性,在示例中为“rpc”

谢谢你的帮助

解决 我将首先介绍我实际实现的解决方案,以及我在项目中使用的解决方案。之后,我将提出一个可以直接添加到简单代码中的更改建议

我实际使用的解决方案: 因为我不想手工定义XML元素,也不想为程序中的每个列表创建转换器,所以我决定使用类的现有注释以及注释的名称属性。我的解决方案适用于使用getter和setter的类,这些类将被序列化!它当前仅绑定到使用
属性
文本
注释的类

为了更加灵活,我创建了一个“基本”类
RequestListConverter
。它有两个受保护的
方法
prepareMethodList
writeRequest
prepareMethodList
将使用反射遍历给定类的所有方法,并创建方法注释映射
writeRequest
然后将指定给方法
prepareMethodList
的类型的单个对象写入到接口
Converter
write
方法中指定的Simple的OutputNode

公共类RequestListConverter{
私有HashMap curMethodAnnotationMap=新HashMap();
@抑制警告(“原始类型”)
受保护的void prepareMethodList(类targetClass){
/*
*首先,从给定的类中获取注释信息。
* 
*因为我们使用getter和setter,所以请寻找“get”方法!
*/
方法[]curMethods=targetClass.getMethods();
对于(方法curMethod:curMethods){
字符串curName=curMethod.getName();
//我们只需要返回字符串的getter方法
if(curName.startsWith(“get”)&&(curMethod.getReturnType()==String.class)){
属性curAttrAnnotation=curMethod.getAnnotation(Attribute.class);
Text curtextnotation=curMethod.getAnnotation(Text.class);
if(curAttrAnnotation!=null){
curMethodAnnotationMap.put(curMethod,curAttrAnnotation);
}否则
if(curtextnotation!=null){
curMethodAnnotationMap.put(curMethod,curTextAnnotation);
}
}
}
}
受保护的void writeRequest(OutputNode curNode,Object curRequest)引发异常{
对于(Map.Entry curMapEntry:curMethodAnnotationMap
.entrySet()){
if((curMapEntry.getKey()==null)
||(curMapEntry.getValue()==null)){
继续;
}
方法curMethod=curMapEntry.getKey();
属性curAttrAnnotation=null;
Text curtextnotation=null;
if(curMapEntry.getValue()instanceof属性){
curAttrAnnotation=(属性)curMapEntry.getValue();
}else if(curMapEntry.getValue()文本实例){
curTextAnnotation=(Text)curMapEntry.getValue();
}否则{
继续;
}
字符串curValue=null;
试一试{
//尝试调用getter
curValue=(字符串)curMethod.invoke(curRequest);
}捕获(IllegalAccessException | IllegalArgumentException
|调用目标异常(e){
//getter方法似乎需要任何参数,奇怪!跳过
//这个!
继续;
}
//如果该方法具有属性批注,则。。。
if(curAttrAnnotation!=null){
布尔值curAttrRequired=curAttrAnnotation.required();
字符串curAttrName=curAttrAnnotation.name();
/*
*如果返回的方法值为NULL,那么如果
*然后抛出NullPointerException,否则跳过
*属性
*/
if(曲线值==null){
如果(需要的话){
抛出新的NullPointerException(
“所需属性”+curAttrName
+“返回空值!”;
}否则{
继续;
}
}
//该属性现在将作为XML文本添加
curNode.setAttribute(curAttrName,curValue);
}否则
//如果该方法具有文本批注,则。。。
if(curtextnotation!=null){
//我们只需要存储它,以便以后创建字符串
curNode.setValue(曲线值);
}
}
curNode.commit();
}
}
基于这个类,我创建了(例如)类
SetRequestListConverter
。它实现了Simple的
Converter
接口,因此它提供了未实现的
read
方法和
write
方法,后者获取可能包含元素或可能为空的列表

该示例显示了
@Root
@Convert(SetRequestListConverter.class)
public abstract class SetRequestList {
    protected ArrayList<SetRequest> requests = new ArrayList<SetRequest>();

    public void add(T newRequest) {
        requests.add(newRequest);
    }
}
public class ClassToSerialize {
    private SetRequestList requestedSets = new SetRequestList();

    @Element(name="get", required=true)
    public SetRequestList getRequestedSets() {
        return requestedSets;
    }

    @Element(name="get", required=true)
    public void setRequestedSets(SetRequestList newRequestedSets) {
        requestedSets = newRequestedSets;
    }
}
<get>
    <set someAttribute="text" anotherAttribute="bla">Some Text</set>
    ...
</get>
<get></get>
public void writeEnd(String name, String prefix) throws Exception {
    String text = indenter.pop();

    // This will act like the element contains text
    if ((last == Tag.START) && (!format.isUseEmptyEndTag())) {
        write('>');
        last = Tag.TEXT;
    }

    if (last == Tag.START) {
        write('/');
        write('>');
    } else {
        if (last != Tag.TEXT) {
            write(text);
        }
        if (last != Tag.START) {
            write('<');
            write('/');
            write(name, prefix);
            write('>');
        }
    }
    last = Tag.END;
}