Putting content:encoded in RSS feed using ROME

99封情书 提交于 2019-12-06 06:02:41

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