SimpleXML:元素'id'已被使用

时间:2013-08-02 21:00:28

标签: java android xml parsing simple-framework

我厌倦了尝试解析一个XML文件。它不是我的,所以我无法编辑它。 首先看一下(我剪掉了不必要的部分)

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>

您可以在第02,06,11和14行看到 ID 标记。问题是我收到错误“元素 ID 已被使用”第14行。
我正在使用SimpleXML以下三个类:

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

1 个答案:

答案 0 :(得分:4)

几乎一切都很好,我看到两个小错误。

第一:

您只需@Root (name = "entry/author")

@Root (name = "author")

你不应该依赖父母将你的课程放在哪里。


第二个

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

应该

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

事实上,我认为你只需要

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

因为您已在List<WhatsNewFeed_entry >

中指定了集合名称

错误是您没有指定要使用的类,您将其指定为字符串,这就是为什么我问的是什么是“条目”您没有告诉序列化程序使用

WhatsNewFeed_entry

之后,它应该适合你


作为一个额外的观点,我真的不喜欢@Root,因为你强迫类具有特定的节点名称,不强制将属性或字段命名为XML预期标记,我认为是一个更好的方法来命名它在元素中,它更清晰,你觉得你正在制作一个真正的XML。然后你就像设计课程一样

我对java不是很好,来自C#世界,但是它非常相似。

我会像这样重写一下:

  //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;
        }

希望它可以帮助您理解缺少的内容