简体   繁体   中英

Yii framework 2.0 add CDATA to Response::FORMAT_XML

I have a page that responses data in XML format. I used this code snippet from Yii 2.0 documentation.

$response = Yii::$app->response;
$response->format = Response::FORMAT_XML;    
$response->data = ['a' => 'AX', 'b' => 'BZ', 'c' => 100];

The result from the above code is.

<response>
   <a>AX</a>
   <b>BZ</b>
   <c>100</c>
</response>

Is there anyway to insert this <![CDATA[ ]]> into the xml tag which contains string or any character as value. What I want to get is.

<response>
   <a>
       <![CDATA[ AX ]]>
   </a>
   <b>
       <![CDATA[ BZ ]]>
   </b>
   <c>100</c>
</response> 

According to Yii's source code and docs it is not possible. When using XmlResponseFormatter to transform your data into XML it will only use DOMText classes as children.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM