2

I'm trying to deserialize an object on an Android device using SimpleXML. In a maven file I used dependency with exclusions (I followed an advice in an another question, as without excluding these dependencies, I couldn't start the application):

<dependency>
    <groupId>org.simpleframework</groupId>
    <artifactId>simple-xml</artifactId>
    <version>2.6.7</version>
     <exclusions>
    <!-- StAX is not available on Android -->
    <exclusion>
        <artifactId>stax</artifactId>
        <groupId>stax</groupId>
    </exclusion>
    <exclusion>
        <artifactId>stax-api</artifactId>
        <groupId>stax</groupId>
    </exclusion>
    <!-- Provided by Android -->
    <exclusion>
        <artifactId>xpp3</artifactId>
        <groupId>xpp3</groupId>
    </exclusion>
</exclusions>

For testing I wrote just a simple class:

class Test {
        String s;
    }

and I try to get an object:

Test t = null;
try {
    t = serializer.read(Test.class, source);
} catch (Exception e) {
    e.printStackTrace();
}

Log.v("t",t.s);

but in the last line, while I try to read the field t.s, I'm getting errors like:

Could not find method javax.xml.stream.XMLInputFactory.newInstance, referenced from method org.simpleframework.xml.stream.StreamProvider.<init>

unable to resolve static method 3372: Ljavax/xml/stream/XMLInputFactory;.newInstance ()Ljavax/xml/stream/XMLInputFactory;

dead code 0x0006-0009 in Lorg/simpleframework/xml/stream/StreamProvider;.<init> 

unable to find class referenced in signature (Ljavax/xml/stream/XMLEventReader;)

What can be the cause of the problem and how can I solve it?

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
krajol
  • 818
  • 3
  • 13
  • 34
  • please let us know what the other question is. Otherwise you might want to remove your excluse and use the latstet Simplxml - framework version and go from there. – Wolfgang Fahl Dec 23 '12 at 23:59
  • @wolfgang-fahl The other question was here: http://stackoverflow.com/questions/5964668/android-error-including-repacking-dependencies-who-reference-javax-core-classes Eventually I used Gson and which I didn't have any problem with. – krajol Dec 27 '12 at 09:20
  • Can you post your xml please? – ollo Jan 04 '13 at 00:07

1 Answers1

1

You have to add annotations to your class. I made a small test with Simple XML 2.6.9 (simple-xml-2.6.9.jar only) --> no problems.

XML:

<test>
   <s>abc</s>
</test>

Test class:

@Root
public class Test
{
    @Element
    String s;


    @Override
    public String toString()
    {
        return "Test{" + "s=" + s + '}';
    }
}

Deserializing XML:

final File f =  ...


Serializer ser = new Persister();
Test t = ser.read(Test.class, f);

System.out.println(t);

Output:

Test{s=abc}
ollo
  • 24,797
  • 14
  • 106
  • 155