放置内容:使用ROME在RSS feed中编码

时间:2012-03-27 10:08:34

标签: java rss rome

我正在尝试使用ROME及其modules将一些HTML内容放在<content:encoded>标记内。到目前为止,我已成功将mediaRSS和geoRSS放入Feed,但我的内容并未显示。

这是我的代码:

ContentModule contentModule = new ContentModuleImpl();
List<ContentItem> contents = new ArrayList<ContentItem>();
List<String> contentValueDOM = new ArrayList<String>();
ContentItem content = new ContentItem();

content.setContentValue("<p>Some text here</p>");
content.setContentEncoding("text/html");
content.setContentAbout("Paragraph");
content.setContentValueDOM(contentValueDOM);
contents.add(content);

contentModule.setContents(contents);
contentModule.setContentItems(contents);
entry.getModules().add(contentModule);

这是我的输出

<item>
  <title>Example page</title>

  <link>http://www.example.com/news/2012/march/example-page.html</link>
  <description>Introduction</description>
  <category>news</category>
  <pubDate>Tue, 27 Mar 2012 08:18:52 GMT</pubDate>
  <guid>http://www.example.com/news/2012/march/example-page.html</guid>
  <dc:date>2012-03-27T08:18:52Z</dc:date>

  <content:items>
    <rdf:Bag>
      <rdf:li>
        <content:item rdf:about="Paragraph">
          <content:encoding rdf:resource="text/html" />
          <rdf:value />
        </content:item>
      </rdf:li>
    </rdf:Bag>

  </content:items>
  <geo:lat>52.09161879618039</geo:lat>
  <geo:long>5.1141280958007655</geo:long>
  <media:content medium="image" fileSize="16029" height="500" type="image/jpeg" width="399" url="http://www.example.com/binaries/content/gallery/image.jpg">
    <media:description type="plain/text" />
    <media:thumbnail url="http://www.example.com/binaries/content/gallery/thumbnail/image.jpg" />
  </media:content>
  <media:content medium="video" expression="full" type="application/x-shockwave-flash" isDefault="true" url="http://www.youtube.com/v/jQq4ju-vupY?rel=0">

    <media:player url="http://www.youtube.com/v/jQq4ju-vupY?rel=0&amp;feature=youtube_gdata_player" width="520" height="390" />
  </media:content>
</item>

1 个答案:

答案 0 :(得分:2)

这似乎有效:

List<String> contents = new ArrayList<String>();
contents.add("<p>Some text here</p>");
ContentModule module = new ContentModuleImpl();
module.setEncodeds(contents);        
entry.getModules().add(module); 

但是,上面使用Updated Syntax而不是Original Syntax输出Feed。使用更新的语法,您可以获得类似的内容(包含&lt; content:encoded&gt; 标记):

<item>
  <content:encoded><![CDATA[<p>Some text here</p>]]></content:encoded>
</item>

当我尝试使用支持原始语法的 ContentItem (使用modules-0.3.2)时,我发现 ContentModuleGenerator 需要 setContentValueDOM 包含要输出的内容的值。此内容似乎也必须可投射 org.jdom.Content (例如,您需要调用 setContentValueDOM(List&lt; org.jdom.Content&gt;) )。由于 org.jdom.CDATA org.jdom.Content 的子类,您可以执行以下操作:

ContentModule contentModule = new ContentModuleImpl();                
List<ContentItem> contents = new ArrayList<ContentItem>();
List<Content> contentValueDOM = new ArrayList<Content>();        
String value = "<p>Some text here</p>";
ContentItem content = new ContentItem();
content.setContentValue(value);
content.setContentAbout("Paragraph"); 
content.setContentFormat("http://www.w3.org/TR/html4/");
CDATA valueElement = new CDATA(value);
contentValueDOM.add(valueElement);
content.setContentValueDOM(contentValueDOM);      
contents.add(content);
contentModule.setContents(contents);
contentModule.setContentItems(contents);
entry.getModules().add(contentModule);

产生:

<item>
  <title>Example page</title>
  <content:items>
    <rdf:Bag>
      <rdf:li>
        <content:item rdf:about="Paragraph">
          <content:format rdf:resource="http://www.w3.org/TR/html4/" />
          <rdf:value><![CDATA[<p>Some text here</p>]]></rdf:value>
        </content:item>
      </rdf:li>
    </rdf:Bag>
  </content:items>
</item>

如果您更改上面的代码示例,用Element替换CDATA部分,并添加适当的格式和编码信息:

//content.setContentFormat("http://www.w3.org/TR/html4/");
//CDATA valueElement = new CDATA(value);
content.setContentFormat("http://www.w3.org/1999/xhtml");
content.setContentEncoding("http://www.w3.org/TR/REC-xml#dt-wellformed");
Element valueElement = new Element("p");
valueElement.setText("Some text here");

您最终会看到显示&lt; content:encoding&gt; 标记的XML:

<item>
  <title>Example page</title>
  <content:items>
    <rdf:Bag>
      <rdf:li>
        <content:item rdf:about="Paragraph">
          <content:format rdf:resource="http://www.w3.org/1999/xhtml" />
          <content:encoding rdf:resource="http://www.w3.org/TR/REC-xml#dt-wellformed" />
          <rdf:value>
            <p>Some text here</p>
          </rdf:value>
        </content:item>
      </rdf:li>
    </rdf:Bag>
  </content:items>
</item>
相关问题