Question

I'm sick and tired trying to parse one XML file. It's not mine, so I can't edit it. Look at it first (I've cut unnecessary parts)

01  <feed>
02    <id>urn:ya.ru:feed/friends/12345678</id>
03    <author>
04      <name>Master</name>
05      <uri>http://test.ya.ru</uri>
06      <y:id>urn:ya.ru:person/12345678</y:id>
07    </author>
08    <title>List of friends posts</title>
09    <updated>2013-08-02T19:14:00Z</updated>
10    <entry>
11      <id>urn:ya.ru:post/110554367/3744</id>
12      <author>
13        <name>MrBigJoker</name>
14        <y:id>urn:ya.ru:person/110554367</y:id>     
15      </author>
16    </entry>
17  </feed>

You can see ID tags in lines 02, 06, 11 and 14. The problem is that I'm receiving an error "Element ID is already used" in line 14.
I'm using SimpleXML with the following three classes:

@Root (name = "feed")
public class Feed { // All lines

    @Element
    private String id;

    @Element
    private Author_feed author;

    @Element
    private String title;

    @Element
    private String updated;

    @ElementList(inline = true, type = String.class, entry = "entry")
    private List<Entry> entries;

@Root (name = "author")
public class Author_feed {  // Lines 03-07

    @Element
    private String name;

    @Element
    private String uri;

    @Element
    @Namespace(prefix = "y")
    private String id;

@Root (name = "entry/author")
class Author_entry {  // Lines 12-15

    @Element
    private String name;

    @Element
    @Path("author")
    @Namespace(prefix = "y")
    private String id;

@Root (name = "entry")
public class WhatsNewFeed_entry { // Lines 10-16

    @Element (name = "id")
    private String id_entry;

    @Element
    private Author_entry author;
Was it helpful?

Solution

Almost everything is good, I see two little mistakes.

First:

You don't need @Root (name = "entry/author") just @Root (name = "author")

You should not depend of the parent of where it is going to be placed your class.


Second One

 @ElementList(inline = true, type = String.class, entry = "entry")
    private List<Entry> entries;

Should Be

 @ElementList(inline = true, type = WhatsNewFeed_entry.class, entry = "entry")
        private List<WhatsNewFeed_entry > entries;

In fact I think you just need

@ElementList(inline = true, entry = "entry")
            private List<WhatsNewFeed_entry > entries;

because you are already specifying the collection name in List<WhatsNewFeed_entry >

The mistake is that you aren't specifying what class to Use, you was Specifying it as a String, that's why I asked what was "Entry" you wasn't telling the Serializer to use the

WhatsNewFeed_entry

After that, it should work for you


As an additional oppinion, I don't really like the @Root because you are forcing the class to have an specific node name neither to force the properties or fields to be named as the XML expected tags , I think is a better approach to name it in the element, its more clear, you feel that you are making a real XML. And you design your classes as you

I'm not very good with java, come from C# world but it's so similar.

I would rewrite it a little like this:

  //First level
            @Root (name = "feed")
            public class Feed { // All lines

                @Element("id")
                private String id;

                @Element("author")
                private Author_feed author;

                @Element("title")
                private String title;

                @Element("updated")
                private String updated;

                @ElementList(inline = true, entry = "entry")
                private List<WhatsNewFeed_entry> entries;
            }

           //Note that there is no root because it was already defined in the first level,
//this way you can use the same class in differents node with different tag names, you 
//could even make an abstract class of author and in one just post the uri class isntead of
// 2 author classes with the same properties
        public class Author_feed {  // Lines 03-07

            @Element("name")
            private String name;

            @Element("uri")
            private String uri;

            @Element("id")
            @Namespace(prefix = "y")
            private String id;
        }


        class Author_entry {  // Lines 12-15

            @Element("name")
            private String name;

            @Element("id")
            @Namespace(prefix = "y")
            private String id;
        }


        public class WhatsNewFeed_entry { // Lines 10-16

            @Element (name = "id")
            private String id_entry;

            @Element("author")
            private Author_entry author;
        }

Hope it helps you to understand what was missing

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top