SimpleXML:元素“id”已被使用

Gro*_*sha 3 java xml parsing android 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>
Run Code Online (Sandbox Code Playgroud)

您可以在第 02、06、11 和 14 行看到ID标签。问题是我在第 14 行收到错误“元素ID已被使用”。
我使用 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;
Run Code Online (Sandbox Code Playgroud)
@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;
Run Code Online (Sandbox Code Playgroud)
@Root (name = "entry/author")
class Author_entry {  // Lines 12-15

    @Element
    private String name;

    @Element
    @Path("author")
    @Namespace(prefix = "y")
    private String id;
Run Code Online (Sandbox Code Playgroud)
@Root (name = "entry")
public class WhatsNewFeed_entry { // Lines 10-16

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

    @Element
    private Author_entry author;
Run Code Online (Sandbox Code Playgroud)

Hec*_*hez 6

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

第一的:

你不需要@Root (name = "entry/author") 只是@Root (name = "author")

您不应该依赖于将在您的班级中放置的位置的父级。


第二个

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

应该

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

事实上,我认为你只需要

@ElementList(inline = true, entry = "entry")
            private List<WhatsNewFeed_entry > entries;
Run Code Online (Sandbox Code Playgroud)

因为您已经在中指定了集合名称 List<WhatsNewFeed_entry >

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

WhatsNewFeed_entry
Run Code Online (Sandbox Code Playgroud)

在那之后,它应该适合你


作为一个额外的意见,我真的不喜欢@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;
        }
Run Code Online (Sandbox Code Playgroud)

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