简单 Xml - 未保留元素顺序?‏

发布于 2024-12-07 06:46:18 字数 376 浏览 0 评论 0原文

我在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海拔太高太耀眼 2024-12-14 06:46:18
import org.simpleframework.xml.Element;    
import org.simpleframework.xml.Order;

    @Order(elements = {"name", "isTrue"})
    public class SimpleXml {

        public static final String NAME = "$NAME$";
        public static final String IS_TRUE = "$IS_TRUE$";

        @Element
        private String name;

        @Element
        private Boolean isTrue;
    ...
import org.simpleframework.xml.Element;    
import org.simpleframework.xml.Order;

    @Order(elements = {"name", "isTrue"})
    public class SimpleXml {

        public static final String NAME = "$NAME$";
        public static final String IS_TRUE = "$IS_TRUE$";

        @Element
        private String name;

        @Element
        private Boolean isTrue;
    ...
江南烟雨〆相思醉 2024-12-14 06:46:18

由于没有答案,我会尽力为到达这里的人节省宝贵的时间。

我没有找到原因,而且由于我没有时间分析简单库,所以我想出了一个“解决方法”。实际上,这更多的是一个建议 - 如果您有一个很大的 xml 定义并且顺序很重要(规则而不是例外),则不要使用它来(封送)创建 xml。无论如何,该顺序主要用于编组,因此只需节省一些时间并手动执行即可。

模板:

<document>
    <name>$NAME
lt;/name>
    <isTrue>$IS_TRUE
lt;/isTrue>
</document>

类:

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;

/**
 * User: ksaric
 */

public class SimpleXml {

    public static final String NAME = "$NAME$";
    public static final String IS_TRUE = "$IS_TRUE$";

    private String name;
    private Boolean isTrue;

    public SimpleXml() {
    }

    public Boolean getTrue() {
        return isTrue;
    }

    public void setTrue(Boolean aTrue) {
        isTrue = aTrue;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        String template = null;

        try {
            template = getTemplate();
        } catch (IOException e) {
            e.printStackTrace();
        }

        /* GUAVA - checkNotNull() */
        if (null == template) return null;

        template = template.replace(NAME, getName());

        /* OR CONVERT IN THE GETTER METHOD */
        template = template.replace(IS_TRUE, getTrue().toString());

        return template;
    }

    /* SINGLETON? Performance(IO) loss... */
    public String getTemplate() throws IOException {
        InputStream templateStream = getClass().getResourceAsStream("/template.xml");

        /* APACHE IO COMMONS */

        /*
         <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
         */

        final String stringTemplate = IOUtils.toString(templateStream);

        return stringTemplate;
    }
}

测试:

import org.junit.Test;

import static junit.framework.Assert.*;

/**
 * User: ksaric
 */

public class SimpleXmlTest {

    @Test
    public void test() throws Exception {
        //Before

        /* Use standard instantiation, factory method recommended for immutability */
        SimpleXml simpleXml = new SimpleXml();
        simpleXml.setName("This is a name");
        simpleXml.setTrue(false);

        //When
        String result = simpleXml.toString();

        //Then
        assertNotNull(result);
        System.out.println(result);
    }
}

不是真正的答案,但可以节省一些时间,不要在 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:

<document>
    <name>$NAME
lt;/name>
    <isTrue>$IS_TRUE
lt;/isTrue>
</document>

The class:

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;

/**
 * User: ksaric
 */

public class SimpleXml {

    public static final String NAME = "$NAME$";
    public static final String IS_TRUE = "$IS_TRUE$";

    private String name;
    private Boolean isTrue;

    public SimpleXml() {
    }

    public Boolean getTrue() {
        return isTrue;
    }

    public void setTrue(Boolean aTrue) {
        isTrue = aTrue;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        String template = null;

        try {
            template = getTemplate();
        } catch (IOException e) {
            e.printStackTrace();
        }

        /* GUAVA - checkNotNull() */
        if (null == template) return null;

        template = template.replace(NAME, getName());

        /* OR CONVERT IN THE GETTER METHOD */
        template = template.replace(IS_TRUE, getTrue().toString());

        return template;
    }

    /* SINGLETON? Performance(IO) loss... */
    public String getTemplate() throws IOException {
        InputStream templateStream = getClass().getResourceAsStream("/template.xml");

        /* APACHE IO COMMONS */

        /*
         <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
         */

        final String stringTemplate = IOUtils.toString(templateStream);

        return stringTemplate;
    }
}

The test:

import org.junit.Test;

import static junit.framework.Assert.*;

/**
 * User: ksaric
 */

public class SimpleXmlTest {

    @Test
    public void test() throws Exception {
        //Before

        /* Use standard instantiation, factory method recommended for immutability */
        SimpleXml simpleXml = new SimpleXml();
        simpleXml.setName("This is a name");
        simpleXml.setTrue(false);

        //When
        String result = simpleXml.toString();

        //Then
        assertNotNull(result);
        System.out.println(result);
    }
}

Not really an answer, but save yourself some time and don't use Simple(which is a great library) on Android...

任谁 2024-12-14 06:46:18

简单 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文