Php 使用CDATA编辑xml文件
我正在使用以下代码编辑xml文件Php 使用CDATA编辑xml文件,php,xml,editing,Php,Xml,Editing,我正在使用以下代码编辑xml文件 $xml = simplexml_load_file($xmlfile) or die ("Unable to load XML file!"); $event->title = $newtitle; $event->description = $newdescription; file_put_contents($xmlfile, $xml->asXML()); $event->description = '<![CDATA[
$xml = simplexml_load_file($xmlfile) or die ("Unable to load XML file!");
$event->title = $newtitle;
$event->description = $newdescription;
file_put_contents($xmlfile, $xml->asXML());
$event->description = '<![CDATA['.$newdescription.']]>';
我想在说明中添加CDATA。我使用了以下代码
$xml = simplexml_load_file($xmlfile) or die ("Unable to load XML file!");
$event->title = $newtitle;
$event->description = $newdescription;
file_put_contents($xmlfile, $xml->asXML());
$event->description = '<![CDATA['.$newdescription.']]>';
用id试试看
$xml = simplexml_load_file($xmlfile,'SimpleXMLElement', LIBXML_NOCDATA);
?我认为当您的需求更加复杂而不再简单时,您需要使用。有一种方法 以下是一个基本示例:
$dom=new DOMDocument();
$xml='data.xml';
$dom->load($xml);
$cdata=$dom->createCDATASection('<p>Foo</p>');
foreach ($dom->getElementsByTagName('item') as $item) {
$item->getElementsByTagName('content')->item(0)->appendChild($cdata);
}
$dom->save($xml);
$dom=newdomdocument();
$xml='data.xml';
$dom->load($xml);
$cdata=$dom->CreateCataSection(“Foo”);
foreach($dom->getElementsByTagName('item')作为$item){
$item->getElementsByTagName('content')->item(0)->appendChild($cdata);
}
$dom->save($xml);
与此相关的XML:
<?xml version="1.0" encoding="utf-8"?>
<data>
<item>
<title>Test</title>
<content><![CDATA[<p>Foo</p>]]></content>
</item>
</data>
试验
Foo]>
关于您的示例和评论,以下内容应能帮助您:
$dom=new DOMDocument();
$dom->validateOnParse = true;
$xml='data.xml';
$dom->load($xml);
// Unless the ID attribute is recognised
foreach ($dom->getElementsByTagName('event') as $event) {
$event->setIdAttribute('id', true);
}
$event = $dom->getElementById("1");
foreach ($event->getElementsByTagName('description') as $description) {
$cdata=$dom->createCDATASection('<p>'. $description->nodeValue .'</p>');
$description->replaceChild($cdata, $description->childNodes->item(0));
}
$dom->save($xml);
$dom=newdomdocument();
$dom->validateOnParse=true;
$xml='data.xml';
$dom->load($xml);
//除非识别ID属性
foreach($dom->getElementsByTagName('event')作为$event){
$event->setIdAttribute('id',true);
}
$event=$dom->getElementById(“1”);
foreach($event->getElementsByTagName('description')作为$description){
$cdata=$dom->CreateCataSection(“”。$description->nodeValue。””);
$description->replaceChild($cdata,$description->childNodes->item(0));
}
$dom->save($xml);
本质上,SimpleXML
将转义XML使用的所有值,因此使用CDATA
节点的原因很多。但是,如果您确实需要它们,则无法在SimpleXML
中完成,请使用另一种替代方法,如DOMDocument
及其功能Erm,尽管您有2个链接而不是我的1个链接,但我相信您早了几秒钟…@Wrikken:sleep defect意味着我不知道时间。我累了会得到奖励吗?你能解释一下我如何使用DOMDocument
和createCDATASection()进行编辑吗
文档中有…@THOmas:我在我的回答中为您添加了一个基本示例。谢谢Cez。我编辑了我的问题。我想用相应的id编辑内容。您能告诉我是怎么回事吗?@THOmas:我添加了一个基于您的XML的示例。希望它能为您指明正确的方向,即以任何方式链接xpath和xquery或dom_导入_simplexml@THOmas:是的,看看DOMXPath@THOmas:如果您希望从代码中获得输出,那么不要使用$dom->save($xml),而是使用$dom->saveXML()。DOMDocument::save写入文件,DOMDocument::saveXML写入字符串