java - 简单的 : Element 'id' is already used

标签 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;

最佳答案

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

首先:

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

你不应该依赖父级来决定你的类(class)的位置。


第二个

 @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 > 中指定了集合名称

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

WhatsNewFeed_entry

在那之后,它应该适合你


补充一点,我不太喜欢 @Root因为你强制类有一个特定的节点名称既不强制属性或字段被命名为 XML 预期的标签,我认为在元素中命名它是一种更好的方法,它更清楚,你觉得你是制作一个真正的 XML。然后你设计你的类(class)

我不是很擅长 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;
        }

希望它能帮助你理解遗漏了什么

关于java - 简单的 : Element 'id' is already used,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18026054/

相关文章:

java - 无法从嵌套 Firebase 数据库获取值

android - 如何在 android studio 中将 2 列中的 6 个图像按钮与 3 行对齐?

xml - XSLT - 通过检查现有属性向现有节点添加新属性

c# - 更多 XML 问题 - 未声明的实体 'nbsp'

java - Java 编译器是否优化了循环局部变量的创建?

java - 检索 JSON 数组并将其放入 java 中的数组中?

java - 如何保护客户端反作弊

Android:在运行时为 child 分配相同的高度

php - PHP SimpleXML-从Youtube XML Feed获取数据

java - 如何在我的java计算器中返回int?