Simple Xml - element order not preserved?

I am using SimpleXml 2.6.1 in my android application. Despite the fact that in the documentation (http://simple.sourceforge.net/download/stream/doc/javadoc/index.html?org/simpleframework/xml/Order.html) the order of elements in xml is similar to the order they are defined in the file class, I always get the order to be random in xml. If I add a few variables, the order of the elements will change again.

Adding @Order notation works, but since the class is complex with 100s of variables, I don’t want to add order. Is this a known bug for Android versions? It works great in java console programs.

ps: I opened the disassembled .class file and found the variables declared in the same order as the java file, so I don't think this is a problem with the class file.

+5
source share
3 answers

Since there is no answer, I will try to save valuable time to everyone who gets here.

I did not find a reason, and since I don’t have time to analyze simple libraries, I came up with "workaroud". Actually this is more advice, do not use it for (marshaling) xml creation if you have a large xml definition and order (rule is more than an exception). An order is mainly used for marshaling, so just save time and do it manually.

Template:

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

Grade:

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;
    }
}

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);
    }
}

, Simple ( ) Android...

+1
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;
    ...
+1

Simple Xml does not save Order on Android. Based on pfh's answer, here is my recommendation: I would prefer to use JAXB if you want to save the order than manual parsing of the string / template. JAXB is a bit more difficult to use than SimpleXml, but comes with a similar set of annotations based on XML serialization and deserialization.

0
source

All Articles