从数据库中转义XML以进行输出

时间:2012-05-24 10:53:30

标签: php html xml zend-framework encoding

我正在尝试使用XML创建zend_form,传递的XML包含所有标记,因此我无法使用htmlspecialchars(),因为标记< >将被编码,我不想要。有没有简单的方法来编码作为标签文本值的元素(标签之间的内容)?

$xml = sprintf('<?xml version="1.0" encoding="UTF-8"?><form><localhost><formmanager><formpage><elements>%s</elements></formpage></formmanager></localhost></form>',
                                $r['form_template']);
$config = new Zend_Config_Xml($xml, 'localhost');
$form = new Zend_Form($config->formmanager->$formName);

保存在DB上的xml的摘录:

<questionTechnicalServices>
<type>MultiCheckbox</type>
<options>
<class>rule2 standard basic2 starter business spotlight</class>
<label>Technical Services</label>
<multioptions>
<item-ContractLaboratoryandtestingServices>Contract Laboratory and testing Services</item-ContractLaboratoryandtestingServices>
<item-ContractRDDesignandEngineering>Contract R&D, Design and Engineering</item-ContractRDDesignandEngineering>
<item-ContractSterilization>Contract Sterilization</item-ContractSterilization>
<item-CoilingRespoolingWinding>Coiling/Respooling/Winding</item-CoilingRespoolingWinding>
<item-OthersTechnicalServices>Others</item-OthersTechnicalServices>
</multioptions>
</options>
</questionTechnicalServices>

&amp;来自R&amp; D,需要更改为&amp;,因为任何&gt;或者&lt;等

此致

1 个答案:

答案 0 :(得分:1)

保存在数据库中的XML无效。元素值应在存储之前进行转义。不这样做会导致你现在遇到的问题。

但是,您可以通过将其作为HTML加载到DOM并将其保存回字符串来更正它。虽然它已经无效,但无法保证任何解析器都能满足您的要求。