Php 使用CDATA的简单XML
我正在尝试使用SimpleXML编写XML,但我需要将html标记保存在XML标记中。 这是我的密码:Php 使用CDATA的简单XML,php,xml,simplexml,cdata,Php,Xml,Simplexml,Cdata,我正在尝试使用SimpleXML编写XML,但我需要将html标记保存在XML标记中。 这是我的密码: class SimpleXMLExtended extends SimpleXMLElement { public function addCData($cdata_text) { $node= dom_import_simplexml($this); $no = $node->ownerDocument; $n
class SimpleXMLExtended extends SimpleXMLElement
{
public function addCData($cdata_text)
{
$node= dom_import_simplexml($this);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection($cdata_text));
}
}
$xml = new SimpleXMLExtended('<?xml version = "1.0" encoding = "UTF-8"?><xml></xml>');
$model->addChild('type')->addCData($type['type']);
$model->addChild('titleXML')->addCData($titleXML['titleXML']);
$model->addChild('features')->addCData($features['features']);
$model->addChild('options')->addCData($options['options']);
$model->addChild('gallery')->addCData($gallery['gallery']);
$model->addChild('info')->addCData($info['info']);
echo('XML: '.$xml->asXML());
类SimpleXMLExtended扩展了SimpleXMLElement
{
公共函数addCData($cdata\U文本)
{
$node=dom\u import\u simplexml($this);
$no=$node->ownerDocument;
$node->appendChild($no->createCDATA节($cdata\U文本));
}
}
$xml=新的SimpleXMLExtended(“”);
$model->addChild('type')->addCData($type['type']);
$model->addChild('titleXML')->addCData($titleXML['titleXML']);
$model->addChild('features')->addCData($features['features']);
$model->addChild('options')->addCData($options['options']);
$model->addChild('gallery')->addCData($gallery['gallery']);
$model->addChild('info')->addCData($info['info']);
echo('XML:'.$XML->asXML());
我不断发现以下错误:
致命错误:对……中的非对象调用成员函数addChild()/第148行的admin/addprocess.php
想法?应该是$xml->addChild(),或者将$xml更改为$modelAhhh以部分修复它。现在它正在写入XML标记,但是只显示第一个字符。@EricKlassen:Where's
$type
array?$type=$\u POST[“type”]$类型不是一个array@EricKlassen如果$type
不是数组,那么为什么要使用$type['type']
从数组中访问密钥?PHP将其解释为通过偏移量从字符串访问字符;由于'type'
不是数字,它会将其强制为0
,这就是为什么您会得到字符串的第一个字符。