简单的Xml – 元素声明两次错误

我一直试图围绕RSS Feed包装一组基于Simple XML(Java Serializer)的类。 样品饲料是

   Coding Horror http://www.codinghorror.com/blog/ programming and human factors - Jeff Atwood en-us Wed, 04 May 2011 20:34:18 -0700 Wed, 04 May 2011 20:34:18 -0700 http://www.typepad.com/ http://blogs.law.harvard.edu/tech/rss  Coding Horror http://sofzh.miximages.com/java/ 100 91 Logo image used with permission of the author. (c) 1993 Steven C. McConnell. All Rights Reserved. http://www.codinghorror.com/blog/      

我在运行代码时遇到的错误是

 org.simpleframework.xml.core.PersistenceException: Element 'link' declared twice at line 24 

并且错误是公平的,因为特定元素名称在xml中出现两次但以不同方式出现。

第一个链接元素在这里

 http://www.codinghorror.com/blog/ 

它直接位于Channel标签下。 然后下一个链接标记再次位于Channel下面,格式如下

在Channel.java类中,我不能有两个具有相同名称链接的变量。 我尝试将变量名称更改为blogLink,并尝试在Element注释中给出名称,Eclipse给了我这个错误

  Change was @Element("name=link") Result is The attribute value is undefined for the annotation Element 

我知道我在这里遗失了一些东西,但是我无法用手指指着它。 我将不胜感激任何帮助。

UPDATE

渠道类

 @Element(name="link") @Namespace(reference="http://www.w3.org/2005/Atom",prefix="atom") private atomlink atomlink; public atomlink getAtomLink() { return atomlink; } 

链接类

  import org.simpleframework.xml.Attribute; import org.simpleframework.xml.Namespace; import org.simpleframework.xml.Root; @Root(name="link") @Namespace(reference="http://www.w3.org/2005/Atom",prefix="atom10") public class atomlink { @Attribute private String rel; public String getRel() { return rel; } 

}

我已经更改了类名,但仍然指向同样的错误。

这是两个不同的元素。 它们的名称空间不同。 了解如何映射命名空间(如果它们完全受到简单XML的支持)。

呃,发现它在文档中:

http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#namesoace

 @Element(name="link") private Link link; @Element(name="link") @Namespace(reference="http://www.w3.org/2005/Atom") private AtomLink atomLink; 

等等。

因为link元素出现两次(虽然它们有不同的命名空间),你需要告诉link分开,自吹是我的全部答案:

1) Rss bean

 package com.example.xyzreader.data.bean; import org.simpleframework.xml.Attribute; import org.simpleframework.xml.Element; import org.simpleframework.xml.ElementList; import org.simpleframework.xml.Namespace; import org.simpleframework.xml.NamespaceList; import org.simpleframework.xml.Root; import org.simpleframework.xml.Text; import java.util.List; /** * @author zmingchun * @version 1.0.0 (2017/3/20) */ @NamespaceList({ @Namespace(prefix = "dc", reference = "http://purl.org/dc/elements/1.1/"), @Namespace(prefix = "atom", reference = "http://www.w3.org/2005/Atom"), @Namespace(prefix = "xhtml", reference = "http://www.w3.org/1999/xhtml"), @Namespace(prefix = "atom10", reference = "http://www.w3.org/2005/Atom"), }) @Root(name = "rss" , strict = false) public class Rss { @Element(name = "channel") public ChannelBean channel; @Attribute public String version; @Override public String toString() { return "Rss{" + "channel=" + channel + ", version='" + version + '\'' + '}'; } @Root(name = "channel", strict = false) public static class ChannelBean { @Element public String title; @ElementList(entry = "link", inline = true, required = false) public List links; @Element public String description; @Element public String language; @Element public String lastBuildDate; @Element public String pubDate; @Element public String generator; @Element public String docs; @Element public ImageBean image; @Element public MetaBean meta; @Override public String toString() { return "ChannelBean{" + "title='" + title + '\'' + ", links=" + links + ", description='" + description + '\'' + ", language='" + language + '\'' + ", lastBuildDate='" + lastBuildDate + '\'' + ", pubDate='" + pubDate + '\'' + ", generator='" + generator + '\'' + ", docs='" + docs + '\'' + ", image=" + image + ", meta=" + meta + '}'; } /** * link bean */ static class Link { @Attribute(required = false) public String href; @Attribute(required = false) public String rel; @Attribute(name = "type", required = false) public String contentType; @Text(required = false) public String link; @Override public String toString() { return "Link{" + "href='" + href + '\'' + ", rel='" + rel + '\'' + ", contentType='" + contentType + '\'' + ", link='" + link + '\'' + '}'; } } @Root(name = "image") public static class ImageBean { @Element public String title; @Element public String url; @Element public String width; @Element public String height; @Element public String description; @Element public String link; @Override public String toString() { return "ImageBean{" + "title='" + title + '\'' + ", url='" + url + '\'' + ", width='" + width + '\'' + ", height='" + height + '\'' + ", description='" + description + '\'' + ", link='" + link + '\'' + '}'; } } @Root(name = "meta") public static class MetaBean { @Attribute(required = false) public String name; @Attribute(required = false) public String content; @Override public String toString() { return "MetaBean{" + "name='" + name + '\'' + ", content='" + content + '\'' + '}'; } } } } 

2) TestSimpleXmlConvert方法

 package com.example.xyzreader.manager; import android.util.Log; import com.example.xyzreader.data.bean.Rss; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.convert.AnnotationStrategy; import org.simpleframework.xml.core.Persister; import org.simpleframework.xml.strategy.Strategy; /** * @author zmingchun * @version 1.0.0 (2017/3/20) */ public class TestSimpleXmlConvert { /** * test convert by simple-xml-2.7.1 */ public static void main(String[] args) { Strategy strategy = new AnnotationStrategy(); Serializer serializer = new Persister(strategy); try { Rss tRss = serializer.read(Rss.class, "\n" + "\n" + "\n" + " Coding Horror\n" + " http://www.codinghorror.com/blog/\n" + " programming and human factors - Jeff Atwood\n" + " en-us\n" + "\n" + " Wed, 04 May 2011 20:34:18 -0700\n" + " Wed, 04 May 2011 20:34:18 -0700\n" + " http://www.typepad.com/\n" + " http://blogs.law.harvard.edu/tech/rss\n" + "\n" + " \n" + " Coding Horror\n" + " http://sofzh.miximages.com/java/\n" + " 100\n" + " 91\n" + " Logo image used with permission of the author. (c) 1993 Steven C. McConnell. All Rights Reserved.\n" + " http://www.codinghorror.com/blog/\n" + " \n" + "\n" + "  \n" + "  \n" + "\n" + "\n" + " "); Log.e(TestSimpleXmlConvert.class.getSimpleName(), "Convert result:"+ tRss.toString()); } catch (Exception e) { e.printStackTrace(); } } } 

您列出的注释格式不正确。

它应该是

 @Element(name="link") 

如果注释具有名为value的单个属性,则可以在不指定键的情况下分配它。 但在这种情况下,您尝试设置的属性是“name”,其值为String类型。

问题的问题是’name’的整个赋值括在括号中,因此它试图将’value’设置为“name = link”,这就是为什么它会爆炸,因为@Element注释没有不指定值属性。

我在使用内容解析xml时遇到同样的问题:

  

我的代码是:

  @Element(name="rating", required=false) @Namespace(prefix="gd", reference="http://schemas.google.com/g/2005") public Rating rating; @Element(name="rating") @Namespace(prefix="yt", reference="http://gdata.youtube.com/schemas/2007") public LikeRating ratingLike; 

这个错误是:

org.simpleframework.xml.core.PersistenceException:字段’ratingLike’上的名称’rating’的复制注释public com.devicefms.android.boardgamesreview.beans.VideoXML $ VideoEntry $ LikeRating com.devicefms.android.boardgamesreview.beans.VideoXML $ VideoEntry.ratingLike at org.simpleframework.xml.core.StructureBuilder.process(StructureBuilder.java:258)

这不是一个修复本身,但我能够通过用一个@ElementList替换我的类中的的2个@Element条目并创建一个对象来解决这个问题。这将满足两种链接类型。 像这样的东西:

 @NamespaceList({ @Namespace(reference="http://www.w3.org/2005/Atom",prefix="atom") }) public class Channel { ... @ElementList(entry="link",inline=true,required=false) public List links ... } public class Link { @Attribute(required=false) public String href; @Attribute(required=false) public String rel; @Attribute(name="type",required=false) public String contentType; @Text(required=false) public String link; } 

这已经解决了吗? 除了马克的回应(使用集合)是否有正确的方法来做到这一点? 我们如何防范未转变为集合的其他领域成为集合?

顺便说一下,我正在使用RSS提要。