@Order 注解对 XML 序列化顺序没有影响 [英] @Order annotation has no effect on the XML serialization order

查看:69
本文介绍了@Order 注解对 XML 序列化顺序没有影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 SimpleXmlConverter 的 Retrofit 2,并且在创建 Soap 请求对象时遇到了一个问题,这基本上是一个具有 4 个元素子元素的元素,每个元素都是不同的数据类型.

I'm using Retrofit 2 with a SimpleXmlConverter and I am facing an issue when creating a Soap Request Object, that is basically an element with 4 element children each one of them being different datatypes.

这是我想要生成的 XML 输出.必须遵守元素顺序:

Here is the XML output I want to produce. The element order must be respected:

 <prf:container>
    <prf:aaa>111111111</prf:aaa>
    <prf:bbb>true</prf:bbb>
    <prf:element>
        <prf:ddd>50</prf:ddd>
        <prf:eee>false</prf:eee>
    </prf:element>
    <prf:ccc>textcontent</prf:ccc>
</prf:container>

现在,这是我的 Android 类 Container.java,表示将被序列化的 Soap 请求对象:

Now, here is my Android Class, Container.java, representing the Soap Request Object that will be serialized:

@Root (name = "prf:container")
@Order(elements={"prf:aaa", "prf:bbb", "prf:element", "prf:ccc"})
public class Container {

    @Element (name = "prf:aaa")
    private int aaa;

    @Element(name = "prf:bbb")
    private boolean bbb;

    @Element (name = "prf:element", required = false)
    private MyElement myElement;

    @Element (name = "prf:ccc", required = false)
    private String ccc;

}

根据 Simple XML 框架文档:

According to the Simple XML framework documentation:

默认情况下,字段的序列化是按声明顺序完成的.

By default serialization of fields is done in declaration order.

然而,在 Android 中,情况并非如此,至少在某些情况下是这样.无论我如何在 Container 类中设置字段声明顺序,输出始终具有相同的元素顺序.这是一个已知错误,并已在其他 SO 帖子中报告.

However, in Android, this is not true, at least in some cases. No matter how I set the field declaration order in my Container class, the output has always the same element order. This is a known bug and as has been reported in other SO posts.

尽管如此,这个问题还是有解决方案的.Order 注释.阅读更多在 Javadoc.

Nonetheless, there is a solution to this issue. The Order annotation. Read more in the Javadoc.

我的问题是在我的情况下使用 Order 注释没有帮助.请注意,我的所有元素的名称都有一个前缀 - prf:.

My problem is that using the Order annotation in my case is not helping. Note that all my elements have a prefix on its name - prf:.

如果我从所有元素名称中删除 prf 前缀,Order 注释将正常工作,并强制 XML 序列化具有定义的顺序.但是输出元素的名称不会带有前缀.

If I remove the prf prefix from all my element names, Order annotation will work properly, and force the XML Serialization to have the defined order. But the output elements won't have the prefix on its name.

但我确实需要我的元素在其名称上带有前缀,否则我的请求将有 500 响应.我还必须在 XML 输出中具有所需的元素顺序.

But I really need my elements to have the prefix on its name, or else my request will have a 500 response. I also have to have the desired element order in my XML output.

有什么解决办法吗?

谢谢

推荐答案

我知道自从您发布这个问题以来,这是一个很长的项目,但是,如果有人遇到同样的问题,我想回答您的问题.我通过执行以下操作解决了同样的问题:

I know it has been a long item since you posted this question but, I would like to answer your question in case anyone faced the same issue. I solved the same issue by doing the following:

对于要按照您想要的顺序使用元素准备的 XML 文档,如果元素有前缀,@Order 注释在某些情况下可能不起作用.在您的情况下,每个元素的 @Order 注释中提到的前缀 'prf' 无法按照您的需要对它们进行排序.

For the XML document to be prepared with the elements in the order you want and if the elements have a prefix, @Order annotation might not work in some cases. In your case, the prefix 'prf' mentioned in the @Order annotation for each element would not work to order them as you desired.

默认情况下,字段的序列化是按声明顺序完成的."

"By default serialization of fields is done in declaration order."

我也不相信这一点,尤其是当您有元素前缀时.因此,我尝试更改 Java 变量名称.我尝试按照在生成的 xml 中需要它们的相同方式按字母顺序命名它们.因此,在您的情况下,您可以按如下方式更改变量名称:

I don't believe this either, especially when you have prefixes for elements. So, I tried changing the Java variable names. I tried naming them in alphabetical order in the same way I needed them in the generated xml. So, in your case, you can change the variable names as follows:

@Root (name = "prf:container")
public class Container {

    @Element (name = "prf:aaa")
    private int element1;

    @Element(name = "prf:bbb")
    private boolean element2;

    @Element (name = "prf:element", required = false)
    private MyElement element3;

    @Element (name = "prf:ccc", required = false)
    private String element4;

}

这将完全按照您的意愿形成 xml 文档.您可能想知道,如果我们将变量名称更改为过于通用,它们并不能代表它们的实际情况,但是,您始终可以使用 getter 和 setter.例如,在您的情况下,您可以:

This would form the xml document exactly as you wanted. You might wonder that if we change the variable names to be too generic, they are not representing what they actually are but, you can always have getters and setters. For example, in your case you can have:

public void setAaa(String aaa){
    this.element1 = aaa;
}

public String getAaa(){
    return element1;
}

以同样的方式,您始终可以使用按字母顺序排列的变量生成类,以确保生成的 xml 具有所需格式的元素.

In the same way you can always generate the classes with alphabetically ordered variables to make sure the generated xml has the elements in the desired format.

这篇关于@Order 注解对 XML 序列化顺序没有影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆