Can';t使用simplexml和php将CDATA添加到我的XML字符串中
我只是想将cdata添加到xml节点描述中。下面是我的xml函数。我已经尝试在我的函数中使用以下函数的位Can';t使用simplexml和php将CDATA添加到我的XML字符串中,php,xml,cdata,Php,Xml,Cdata,我只是想将cdata添加到xml节点描述中。下面是我的xml函数。我已经尝试在我的函数中使用以下函数的位 <?php function updateXMLFile($itemName, $description, $pageName, $imageFileName) { $imageSrc = "<img src='http://nicolaelvin.com/authoring/phpThumb/phpThumb.php?src=../images/" . $i
<?php
function updateXMLFile($itemName, $description, $pageName, $imageFileName)
{
$imageSrc = "<img src='http://nicolaelvin.com/authoring/phpThumb/phpThumb.php?src=../images/" . $imageFileName . "&w=100'/>";
$id = strtolower($id = str_replace(' ', '_', $itemName));
$directLinkToItem = 'http://nicolaelvin.com/authoring/' . $pageName . '.php#' . $id;
$xml = simplexml_load_file('nicolaElvinsPortfolio.xml');
$item = $xml->channel->addChild('item');
$item->addChild('title', $itemName);
$item->addChild('pubDate', date('r'));
$item->addChild('link', $directLinkToItem);
$item->addChild('description');
$cdata->description->createCDATASection('testyfhgjhsgsdjahgs');
$item->appendChild($cdata);
///Format XML to save indented tree rather than one line
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
//Save XML to file - remove this and following line if save not desired
$dom->save('nicolaElvinsPortfolio.xml');
}
//function from php.net
function sxml_cdata($path, $string)
{
$dom = dom_import_simplexml($path);
$cdata = $dom->ownerDocument->createCDATASection($string);
$dom->appendChild($cdata);
}
?>
channel->addChild('item');
$item->addChild('title',$itemName);
$item->addChild('pubDate',date('r'));
$item->addChild('link',$directLinkToItem);
$item->addChild('description');
$cdata->description->createCDATA节('testyfhghjhsgsdjahgs');
$item->appendChild($cdata);
///格式化XML以保存缩进树而不是一行
$dom=新的DOMDocument('1.0');
$dom->preserveWhiteSpace=false;
$dom->formatOutput=true;
$dom->loadXML($xml->asXML());
//将XML保存到文件-如果不需要保存,请删除此行和下面的行
$dom->save('nicolaelvinspolifolio.xml');
}
//来自php.net的函数
函数sxml\u cdata($path,$string)
{
$dom=dom\u import\u simplexml($path);
$cdata=$dom->ownerDocument->CreateCataSection($string);
$dom->appendChild($cdata);
}
?>
试试这件衣服的尺寸。如果您对此有任何问题/疑问,请告诉我(已修复)
函数updateXMLFile($itemName、$description、$pageName、$imageFileName){
//将要使用的文件的路径
$filePath='nicolaElvinsPortfolio.xml';
//创建链接-不要忘记使用urlencode()、htmlspecialchars()等适当地转义值
$imageSrc=“”;
$directLinkToItem='1!'http://nicolaelvin.com/authoring/“.urlencode($pageName)。”.php#“.urlencode(strtolower(str_replace(“,”,$itemName)));
//创建CDATA值-无论您希望它看起来像什么
$cdata=“$description:$imageSrc”;
//创建一个文档
$dom=新的DOMDocument('1.0');
$dom->preserveWhiteSpace=false;
$dom->formatOutput=true;
//将数据从文件加载到DOMDocument
如果(!$dom->load($filePath))引发新异常(“无法加载数据源文件“$filePath”);
//创建新文档并将其添加到文档中
$item=$dom->getElementsByTagName('channel')->item(0)->appendChild(新的DOMElement('item'));
//添加的子元素
$item->appendChild(新的domeElement('title',$itemName));
$item->appendChild(新的domeElement('pubDate',date('r'));
$item->appendChild(新的domeElement('link',$directLinkToItem));
//添加CDATA
$item->appendChild(新的domeElement('description'))->appendChild(新的domcataSection($cdata));
//现在保存回文件
$dom->save($filePath);
}
注意:如果
DOMDocument::load()
失败,这将引发异常-不要忘记捕获它 我已经尝试了其中的createCDATA部分,这是我从php.net中的函数改编而来的。我没有收到任何错误,只是没有向我的XML文档添加任何内容。我不确定simpleXML是否支持该功能。顾名思义,它用于XML的简单操作。你可能不得不求助于完整的DOM类。哦,好的,谢谢,我不知道怎么做,所以我可能不得不忽略它。你确定没有任何错误吗?具体地说,您确定在尝试对非对象调用方法“createCDATASection()”的过程中没有遇到E_致命错误吗?我询问的原因是,您从未在updateXMLFile()
函数的作用域中定义$cdata
。请查看这是否有效,或者-
function updateXMLFile($itemName, $description, $pageName, $imageFileName) {
// Path to file that will be used
$filePath = 'nicolaElvinsPortfolio.xml';
// Create links - don't forget to escape values appropriately with urlencode(), htmlspecialchars() etc
$imageSrc = "<img src='".htmlspecialchars('http://nicolaelvin.com/authoring/phpThumb/phpThumb.php?src=../images/'.urlencode($imageFileName).'&w=100')."'/>";
$directLinkToItem = 'http://nicolaelvin.com/authoring/'.urlencode($pageName).'.php#'.urlencode(strtolower(str_replace(' ', '_', $itemName)));
// Create the CDATA value - whatever you want this to look like
$cdata = "$description: $imageSrc";
// Create a DOMDocument
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
// Load data from file into DOMDocument
if (!$dom->load($filePath)) throw new Exception("Unable to load data source file '$filePath'");
// Create the new <item> and add it to the document
$item = $dom->getElementsByTagName('channel')->item(0)->appendChild(new DOMElement('item'));
// Add the <item>'s sub elements
$item->appendChild(new DOMElement('title', $itemName));
$item->appendChild(new DOMElement('pubDate', date('r')));
$item->appendChild(new DOMElement('link', $directLinkToItem));
// Add the CDATA
$item->appendChild(new DOMElement('description'))->appendChild(new DOMCdataSection($cdata));
// Now save back to file
$dom->save($filePath);
}