此代码生成以下o / p
$dom = new DomDocument("1.0", "ISO-8859-1");
$root = $dom->appendChild($dom->createElement( "Questions"));
$sxe = simplexml_import_dom( $dom );
$question = $sxe->addchild("Question");
$question->addAttribute('id', $Question_Id);
$question->addAttribute('type', $type);
$question->addChild('Option_One', $Option_One);
$dom->formatOutput = true;
$xmlString = $dom->saveXML();
$dom->save("{$Campaign_Name}.xml");
o / p是
<?xml version="1.0" encoding="ISO-8859-1"?>
<Questions>
<Question id="1" type="mcma">
<Option_One>Bhuntar</Option_One>
</Question>
</Questions>
现在我想将CDATA添加到问题中。
答案 0 :(得分:0)
不要混用不同的对象类型,你会没事的
$dom = new DomDocument("1.0", "ISO-8859-1");
$root = $dom->appendChild( $questions = $dom->createElement( "Questions"));
$questions->appendChild($question = $dom->createElement('Question'));
$question->setAttribute('id', $Question_Id);
$question->setAttribute('type', $type);
$question->appendChild($dom->createElement('Option_One', $Option_One));
$question->appendChild($dom->createCDATASection( $Questiontext));
$dom->formatOutput = true;
$xmlString = $dom->saveXML();
$dom->save("{$Campaign_Name}.xml");