Putting content:encoded in RSS feed using ROME

久未见 提交于 2019-12-08 02:51:28

问题


I'm trying to put some HTML content inside <content:encoded> tags using ROME and its modules. So far I've succesfully put mediaRSS and geoRSS in the feed, but my content is not showing up.

Here's my code:

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

And here is my output

<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:


This seems to work:

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

However the above outputs the feed using the Updated Syntax rather than the Original Syntax. With the Updated Syntax you get something that looks like (this contains the <content:encoded> tag):

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

When I tried to use the ContentItem which does support Original Syntax (using modules-0.3.2) like you did I found that the ContentModuleGenerator required that setContentValueDOM contains the value of the content to output. It also appears that this content needs to be castable to org.jdom.Content (e.g. you need to call setContentValueDOM(List<org.jdom.Content>)). As org.jdom.CDATA is a subclass of org.jdom.Content you can do something like this:

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

which produces:

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

If you alter the above code sample replacing the CDATA section with an Element and adding appropriate format and encoding information thus:

//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");

you will end up with XML showing the <content:encoding> tag:

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


来源:https://stackoverflow.com/questions/9887432/putting-contentencoded-in-rss-feed-using-rome

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!