2011-05-12 51 views
6

我一直在尝试基于简单的XML(Java序列化)围绕RSS源来包装一组类。样品饲料简单的Xml-元素声明的两次错误

<?xml version="1.0" encoding="UTF-8"?> 
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"> 
<channel> 
    <title>Coding Horror</title> 
    <link>http://www.codinghorror.com/blog/</link> 
    <description>programming and human factors - Jeff Atwood</description> 
    <language>en-us</language> 

    <lastBuildDate>Wed, 04 May 2011 20:34:18 -0700</lastBuildDate> 
    <pubDate>Wed, 04 May 2011 20:34:18 -0700</pubDate> 
    <generator>http://www.typepad.com/</generator> 
    <docs>http://blogs.law.harvard.edu/tech/rss</docs> 

    <image> 
     <title>Coding Horror</title> 
     <url>http://www.codinghorror.com/blog/images/coding-horror-official-logo-small.png</url> 
     <width>100</width> 
     <height>91</height> 
     <description>Logo image used with permission of the author. (c) 1993 Steven C. McConnell. All Rights Reserved.</description> 
     <link>http://www.codinghorror.com/blog/</link> 
    </image> 

    <xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" /> 
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/codinghorror" />   

</channel> 
</rss> 

,我得到运行代码时是

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

而且由于特定元素名称的XML,但以不同的方式出现两次错误是不够公平的错误。

第一个链接元素在这里直接

<link>http://www.codinghorror.com/blog/</link> 

其频道标签下。再下一个链接标记又道下按以下格式

<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/codinghorror" />

在Channel.java I类不能有相同名称的链接的两个变量。我试图改变一个变量名blogLink并在要素标注试图给人名称和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; 
} 

Link类

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

}

我已经改变了类名,但它仍然指向相同的错误。

+0

这是否解决了?我正试图为simple-xml映射出RSS类。如果你能分享,会很好。 – 2011-05-31 16:16:45

+1

不,我不能。发送邮件到邮件列表并没有得到答复。根据文档命名空间应该做的伎俩,但它不适合我。由于时间是我的一个限制,我用dom xml实现了我自己的自定义rss包装器。 – vikramjb 2011-06-01 03:08:43

回答

1

这些是两个不同的元素。它们不同于名称空间。了解如何映射名称空间(如果它们完全由简单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; 

等。

+1

我做到了这一点,我得到了同样的错误“元素”链接“在24行声明两次”。我在原始帖子中添加了我的代码修改。感谢您的回复,非常感谢。 – vikramjb 2011-05-12 12:22:35

+0

(更新我的示例以显示它将如何显示)。 – 2011-05-14 04:11:31

+0

此外,您不应该提供前缀,因为它可能会根据每封邮件而有所不同;只有命名空间是相关的。 – 2011-05-14 04:12:17

0

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

应该

@Element(name="link") 

如果注解有一个名为值一个属性,它可以在不指定密钥分配。但是在这种情况下,你试图设置的属性是'name',它的值是String类型的值。

从问题的问题是,'name'的整个赋值被括在圆括号中,因此它试图将'value'设置为“name = link”,这就是它被炸毁的原因,因为@Element注释不指定值属性。

+0

这是我的一个错字。 Neverthless我改变了,它给了我一个不同的错误。感谢您的回复,欣赏它。 – vikramjb 2011-05-12 12:23:16

0

我有同样的问题解析XML的内容:

<gd:rating average='4.9304347' max='5' min='1' numRaters='230' rel='http://schemas.google.com/g/2005#overall'/><yt:statistics favoriteCount='0' viewCount='43378'/><yt:rating numDislikes='4' numLikes='226'/> 

我的代码是:

 @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.b eans.VideoXML $ VideoEntry.ratingLike at org.simpleframework.xml.core.StructureBuilder.process(StructureBuilder.java:258)

0

这不是一个固定的问题,但我能够解决这个问题用我的类中的一个@ElementList替换< atom中的2 @Element条目:link/>和< link/>,然后创建一个满足两种链接类型的对象。类似这样的:

@NamespaceList({ 
    @Namespace(reference="http://www.w3.org/2005/Atom",prefix="atom") 
}) 
public class Channel { 

... 

@ElementList(entry="link",inline=true,required=false) 
public List<Link> 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; 
} 
+0

我第二个这个,它的工作,等待错误被纠正。 – 2014-11-04 13:10:31

0

这个问题解决了吗?除了Mark的回复(使用集合)之外,还有这样一个适当的方法吗?我们如何防范没有变成收藏品的其他领域成为收藏品?

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

1

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

1)Rss

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<Link> 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, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
        "<rss xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:atom=\"http://www.w3.org/2005/Atom\" version=\"2.0\">\n" + 
        "<channel>\n" + 
        " <title>Coding Horror</title>\n" + 
        " <link>http://www.codinghorror.com/blog/</link>\n" + 
        " <description>programming and human factors - Jeff Atwood</description>\n" + 
        " <language>en-us</language>\n" + 
        "\n" + 
        " <lastBuildDate>Wed, 04 May 2011 20:34:18 -0700</lastBuildDate>\n" + 
        " <pubDate>Wed, 04 May 2011 20:34:18 -0700</pubDate>\n" + 
        " <generator>http://www.typepad.com/</generator>\n" + 
        " <docs>http://blogs.law.harvard.edu/tech/rss</docs>\n" + 
        "\n" + 
        " <image>\n" + 
        "  <title>Coding Horror</title>\n" + 
        "  <url>http://www.codinghorror.com/blog/images/coding-horror-official-logo-small.png</url>\n" + 
        "  <width>100</width>\n" + 
        "  <height>91</height>\n" + 
        "  <description>Logo image used with permission of the author. (c) 1993 Steven C. McConnell. All Rights Reserved.</description>\n" + 
        "  <link>http://www.codinghorror.com/blog/</link>\n" + 
        " </image>\n" + 
        "\n" + 
        " <xhtml:meta xmlns:xhtml=\"http://www.w3.org/1999/xhtml\" name=\"robots\" content=\"noindex\" /> \n" + 
        " <atom10:link xmlns:atom10=\"http://www.w3.org/2005/Atom\" rel=\"self\" type=\"application/rss+xml\" href=\"http://feeds.feedburner.com/codinghorror\" />  \n" + 
        "\n" + 
        "</channel>\n" + 
        " </rss>"); 
      Log.e(TestSimpleXmlConvert.class.getSimpleName(), "Convert result:"+ tRss.toString()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
}