我知道有很多用php创建cdata的例子,但我还没有找到一个在我的情况下有帮助的。我需要创建一个xml文件,将由php以外的东西使用。我需要在xml中创建cdata,它将包含一个要使用的函数。最终的xml文件应该如下所示。
<?xml version="1.0" encoding="utf-8" ?>
<component name="Test" extends="out" >
<script type="text" >
<![CDATA[
function init()
m.content = createObject("RoSGNode","ContentNode")
m.top.setFocus(true)
dateNow = CreateObject("roDateTime")
dateNow = dateNow.asSeconds() - 2000
addItemName($Iname)
end function
]]>
</script>
</component>
创建xml的代码。我只是不知道如何创建cdata info。如有任何帮助,我将不胜感激。
$xml=new DOMDocument('1.0', 'UTF-8');
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$components = $xml->createElement("components");
$name=$xml->createAttribute("name");
$name->value = "Test";
$extends=$xml->createAttribute("extends");
$extends->value = "out";
$components->appendChild($name);
$components->appendChild($extends);
$script = $xml->createElement("script");
$type=$xml->createAttribute("type");
$type->value = "text";
$script->appendChild($type);
$components->appendChild($script);
$xml->appendChild($components);
$xml->save($filename2);
经过一番努力,我终于能把cdata放进去了。
$cdata = $xml->createCDATASection("function init()");
$script->appendChild($cdata);