【问题标题】:Avoiding character encoding in Apache Abdera避免在 Apache Abdera 中进行字符编码
【发布时间】:2013-06-17 12:39:19
【问题描述】:

我正在编写一个简单的文章编辑器,它将与 CMS 系统一起使用,该系统提供 Atom API 来添加/编辑文章。为了与 CMS 通信,我使用了 Apache Abdera 库。但是我遇到了字符编码的问题。发送到 CMS 的数据将被编码如下:

<entry>
  <content xmlns:vdf="http://www.vizrt.com/types" type="xml">
    <vdf:payload>
      <vdf:field name="body">
        <vdf:value>&lt;div xmlns="http://www.w3.org/1999/xhtml">&lt;p>Text comes here&lt;/p>&lt;/div></vdf:value>
      </vdf:field>
    </vdf:payload>
  </content>
</entry>

但是 CMS 系统需要这个:

<entry>
  <content xmlns:vdf="http://www.vizrt.com/types" type="xml">
    <vdf:payload>
      <vdf:field name="body">
        <vdf:value><div xmlns="http://www.w3.org/1999/xhtml"><p>Text comes here</p></div></vdf:value>
      </vdf:field>
    </vdf:payload>
  </content>
</entry>

换句话说,没有字符转义。有谁知道如何使用 Apache Abdera 来实现这一点?

【问题讨论】:

    标签: java atom-feed apache-abdera


    【解决方案1】:

    我对 abdera 的内部结构并不完全熟悉,因此无法准确解释这里发生了什么,但我认为本质是如果你不想让 abdera 逃跑,你就不能使用字符串或纯文本作为值东西。相反,您必须使用 Element 和 abdera-type XHtml

    这样的事情对我有用:

    String body = "<p>Text comes here</p>"
    
    //put the text into an XHtml-Element (more specificly an instance of Div)
    //I "misuse" a Content object here, because Content offers type=XHtml. Maybe there are better ways.
    Element el = abdera.getFactory().newContent(Content.Type.XHTML).setValue(body).getValueElement();
    
    //now create your empty <vdf:value/> node.
    QName valueQName = new QName("http://your-vdf-namespace", "value", "vdf");
    ExtensibleElement bodyValue = new ExtensibleElementWrapper(abdera.getFactory(),valueQName);
    
    //now attach the Div to that empty node. Not sure what's happening here internally, but this worked for me :)
    bodyValue.addExtension(el);
    

    现在,bodyValue 可以用作字段的值,Abdera 应该正确渲染所有内容。

    【讨论】:

      猜你喜欢
      • 2015-05-02
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      • 2014-03-31
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多