1

In one of my applications I'm using thegamesdb.net's APIs to download the cover images of some games. To get informations from XML files I'm trying to implement Simple XML library to deserialize.

This is my example query result (name=Splatterhouse,platform=TurboGrafx 16):

<?xml version="1.0" encoding="UTF-8" ?>
<Data>
    <Game>
        <id>3776</id>
        <GameTitle>Splatterhouse</GameTitle>
        <ReleaseDate>01/01/1990</ReleaseDate>
        <Platform>TurboGrafx 16</Platform>
    </Game>
</Data>

And this is how I implemented my classes in java:

@Root
public class Data{
    @ElementList(inline=true)
    private List<Game> list; //This is correct, it's only in the example that I've only one result.
    public List<Game> getGames(){
        return list;
    }
}
@Root
public class Game{
    @Element
    private int id;
    @Element
    private String GameTitle;
    @Element(required=false)
    private String ReleaseDate;
    @Element(required=false)
    private String Platform;
    public String getTitle(){
        return GameTitle;
    }
    public int getId(){
        return id;
    }
}

To read from the xml file, I call the serializer:

String xml = getXmlFromUrl(URL);
Serializer serializer = new Persister();
Data data = serializer.read(Data.class, xml);

What's wrong?

UPDATE: It came out that the tutorials on Simple Site are a bit incomplete since they don't set properly annotations, these annotations solved all my issues, plus all the warnings are "normal", it's simple stating that jaxp doesn't exist on android and is going to use other tools (XmlPullParser?).

This is my proper code:

@Root(name = "Data")
public class Data {
    @ElementList(inline = true, required = false)
    private List<Game> list = new ArrayList<Game>();

    public List<Game> getGames() {
        return list;
    }
}

@Root(name = "Game", strict = false)
public class Game {
    @Element(name = "id")
    private int id;
    @Element(name = "GameTitle")
    private String GameTitle;

    public String getTitle() {
        return GameTitle;
    }

    public int getId() {
        return id;
    }
}

4 Answers 4

1

You could also use JAXB to (de)serialize XMLs (or to (un)marshal as they call it). It comes with java - so there's no need for an additional library. Here you have an example.

Sign up to request clarification or add additional context in comments.

7 Comments

What about performance? I read many answers saying that simpleXML is fast enough.
I don't get your point. If performance is your main concern, then de/serialize to JSON or google's protobuf instead of using XML. I never had any performance troubles with JAXB.
I am using XML apis, ho am i supposed to deserialize them to json/protobuf?
With JAXB it's easy to deserialize to JSON since it uses the same annotations as for XML. For using protobuf I suggest you read a tutorial/google. If you don't care how the serialized form of the data looks like, googles protobuf gives you the biggest performance boost. developers.google.com/protocol-buffers/docs/javatutorial for jaxb follow the link in the answer...
I got simpleXML to work, but I was curious to try also JAXB so I tried to make my implementation, but the annotations didn't work and i couldn't import them. End of the story: JAXB is not included in android and its jar is 9MB. That's why on android most of people use SimpleXML, even if JAXB is a Enterprise Standard from what I read, so it should be better. :)
|
1

I believe you need to declare your classes in their own files (Data and Game), or mark them as static. If you declare them as inner classes they can't be constructed by the library because they can only exist within an instance of the parent class.

1 Comment

Yes , the issue was only unproper annotations... snippets on the simple website are incomplete...
0

It's nothing to do with your XML, it seems like something is wrong with the library you're using (not being able to create a new instance of a certain class). Consider re-downloading it and adding it to your project again.

1 Comment

0

The tutorials on SimpleXML website show an incomplete and inaccurate use of annotations. I updated my code, check at the bottom of my answer to get the proper implementation. You're strictly adviced to use name="Name" in your annotations, and if you don't need all the Elements/Attributes use strict=false on the class and required=false on your elements or attributes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.