3

我在 android 上遇到了 simplexml 框架的奇怪问题。我试图从 xml 源中读取并填充一个名为 weatherdata 的对象。

XML 文件(对此没有控制):

<weatherdata>
    <product class="pointData">
        ....
    </product>
</weatherdata>

所以我的java文件看起来像:

@Root(name = "weatherdata", strict=false)
public class Weatherdata {

    @Element(name="product", required = true)
    protected ProductType product;

但我得到一个非常奇怪的错误:

01-14 14:22:28.919: W/System.err(18011): java.lang.ClassNotFoundException: pointData in loader dalvik.system.PathClassLoader

如果我尝试在 @Element 字段中使用 class=ProductType.class,它并不在乎。即使我使用构造函数在我的项目中创建 pointData 类,错误也不会改变。它看起来像“class”作为保留关键字与简单的 xml 框架 2.6.2 混淆。

我无法更改xml。知道我该如何解决吗?提前谢谢。

4

1 回答 1

7

尝试使用 TreeStrategy 将“类”替换为其他内容。

Strategy strategy = new TreeStrategy("clazz", "len"); // clazz substitute for class,
                                                      // and len for array lengths
Serializer serializer = new Persister(strategy);
于 2012-01-24T14:22:43.963 回答