Simple XML framework on android, class attribute

2019-05-09 16:40发布

问题:

Ive got a weird problem with simplexml framework on android. Im trying to read and fill an object called weatherdata from a xml source.

XML File (no control on this one):

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

So my java file looks like :

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

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

But I get a very weird error :

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

If i try to class=ProductType.class in the @Element field, it doesn't care. Even when I create the pointData class in my projet with a constructor, the error doesnt change. It looks like "class" as a reserved keyword mess with simple xml framework 2.6.2.

I can't change the xml. Any idea how I could solve that ? thanks ahead.

回答1:

Try using TreeStrategy to substitute the "class" for something else.

Strategy strategy = new TreeStrategy("clazz", "len"); // clazz substitute for class,
                                                      // and len for array lengths
Serializer serializer = new Persister(strategy);