2

我正在尝试使用 SimpleXML 反序列化 Android 设备上的对象。在一个 Maven 文件中,我使用了排除项的依赖项(我在另一个问题中遵循了建议,因为不排除这些依赖项,我无法启动应用程序):

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

为了测试,我只写了一个简单的类:

class Test {
        String s;
    }

我试图得到一个对象:

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

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

但在最后一行,当我尝试读取字段 ts 时,我收到了如下错误:

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

问题的原因可能是什么,我该如何解决?

4

1 回答 1

1

您必须为您的课程添加注释。我用 Simple XML 2.6.9(simple-xml-2.6.9.jar仅限)做了一个小测试——>没问题。

XML:

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

测试类:

@Root
public class Test
{
    @Element
    String s;


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

反序列化 XML:

final File f =  ...


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

System.out.println(t);

输出:

Test{s=abc}
于 2013-01-04T00:06:42.943 回答