简单 Xml - 未保留元素顺序?
我在我的 Android 应用程序中使用 SimpleXml 2.6.1。尽管文档(http://simple.sourceforge.net/download/stream/doc/javadoc/index.html?org/simpleframework/xml/Order.html)说xml中元素的顺序与方式相同它们在类文件中定义,我总是在 xml 中得到随机的顺序。如果我添加更多变量,元素的顺序会再次改变。
添加 @Order 表示法是可行的,但由于该类很复杂,有数百个变量,所以我不想添加顺序。这是 Android 版本的已知错误吗?它在 java 控制台程序中运行良好。
ps:我反汇编打开.class文件,发现变量声明的顺序与java文件相同,所以我不认为这是一个class文件问题。
I am using SimpleXml 2.6.1 in my android app. Eventhough the documentation (http://simple.sourceforge.net/download/stream/doc/javadoc/index.html?org/simpleframework/xml/Order.html) says the order of the elements in the xml are same as the way they have defined in the class file, I am always getting the order to be random in the xml. If I add few more variables, the order of the elements again changes.
Adding @Order notation works, but since the class is complex with 100s of variables, I do not want to add order. Is this a known bug for android versions? It works fine in java console programs.
p.s: I opened the .class file disassembled and found the variables declared in the same order as java file, so I don't think it's a class file issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于没有答案,我会尽力为到达这里的人节省宝贵的时间。
我没有找到原因,而且由于我没有时间分析简单库,所以我想出了一个“解决方法”。实际上,这更多的是一个建议 - 如果您有一个很大的 xml 定义并且顺序很重要(规则而不是例外),则不要使用它来(封送)创建 xml。无论如何,该顺序主要用于编组,因此只需节省一些时间并手动执行即可。
模板:
类:
测试:
不是真正的答案,但可以节省一些时间,不要在 Android 上使用 Simple(这是一个很棒的库)...
Since there is no answer, I'll try to save precious time to anyone who gets here.
I found no cause, and since I don't have time to analyze Simple libraries, I came up with a "workaroud". It's more of an advice, actually - don't use it for (marshaling)creating xml if you have a large xml definition and the order matters(a rule more than an exception). The order is mostly used for marshaling anyway so just save yourself some time and do it manually.
The template:
The class:
The test:
Not really an answer, but save yourself some time and don't use Simple(which is a great library) on Android...
简单 Xml 不会在 Android 上保留订单。根据pfh的回答,我的建议是:
在您希望保留顺序而不是手动字符串/模板解析的情况下,我更愿意使用 JAXB。 JAXB 使用起来比 SimpleXml 稍微复杂一些,但带有类似的基于 xml 序列化和反序列化的注释集。
Simple Xml doesn't preserve Order on Android. Based on pfh's answer, here is my recommendation:
I would prefer to use JAXB in the case where you want the order to be preserved than manual string/template parsing. JAXB is slightly complex to use than SimpleXml but comes with similar set of annotations based xml serialization and deserialization.