6

Simple XML (Java Serializer) に基づく一連のクラスを RSS フィードにラップしようとしています。サンプルフィードは

<?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 で 2 回発生しますが、異なる方法で発生するため、エラーは十分に公平です。

最初のリンク要素はこちら

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

Channel タグのすぐ下にあります。そして、次のリンクタグは、次の形式でチャネルの下に再びあります

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

Channel.java クラスでは、同じ名前のリンクを持つ 2 つの変数を持つことはできません。変数名を blogLink に変更して、Element アノテーションに名前を付けようとしたところ、Eclipse でこのエラーが発生しました

 Change was

@Element("name=link")


Result is

The attribute value is undefined for the annotation Element

ここで何かが欠けていることは知っていますが、指を置くことはできません。これについて何か助けていただければ幸いです。

アップデート

チャネル クラス

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

}

クラス名を変更しましたが、それでも同じエラーを指しています。

4

6 に答える 6

1

これらは 2 つの異なる要素です。名前空間によって異なります。名前空間をマップする方法を確認してください (単純な 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;

等々。

于 2011-05-12T03:44:42.113 に答える
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」パブリック com.devicefms.android.boardgamesreview.beans.VideoXML$VideoEntry$LikeRating com.devicefms.android.boardgamesreview.beans.VideoXML$ の名前「rating」の重複した注釈VideoEntry.ratingLike at org.simpleframework.xml.core.StructureBuilder.process(StructureBuilder.java:258)

于 2012-10-04T15:37:08.427 に答える
0

リストした注釈は適切にフォーマットされていません。

そのはず

@Element(name="link")

注釈に value という名前の単一のプロパティがある場合、キーを指定せずに割り当てることができます。ただし、この場合、設定しようとしているプロパティは、String 型の値を持つ「name」です。

質問からの問題は、「名前」の割り当て全体が括弧で囲まれているため、「値」を「名前=リンク」に設定しようとしていたため、@Elementアノテーションが値プロパティを指定しないでください。

于 2011-05-12T03:45:02.677 に答える
0

これ自体は修正ではありませんが、クラスの <atom:link/> と <link/> の 2 つの @Element エントリを単一の @ElementList に置き換え、オブジェクトを作成することで、この問題を回避できました。両方のリンク タイプを満たします。このようなもの:

@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;
}
于 2012-10-26T19:14:54.017 に答える