【问题标题】:Can't add CDATA into my XML string using simplexml with php无法使用带 php 的 simplexml 将 CDATA 添加到我的 XML 字符串中
【发布时间】:2012-05-04 08:54:25
【问题描述】:

我只是想将 cdata 添加到 xml 节点 - 描述。我的xml函数如下。我尝试在我的函数中对php.net 使用以下函数的位

<?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);
}
?>

【问题讨论】:

  • 我已经尝试了其中说创建 CDATASection 的位,它是我从 php.net 的函数改编而来的。我没有收到任何错误,只是没有向我的 XML 文档添加任何内容。
  • 我不确定 simpleXML 是否支持该功能。顾名思义,它用于对 XML 的简单操作。您可能不得不求助于完整的 DOM 类。
  • 哦,好的,谢谢,我不知道该怎么做,所以我可能不得不把它排除在外
  • 您确定没有收到任何错误吗?具体来说,您确定您没有按照Attempt to call method 'createCDATASection()' on a non-object 的方式获得 E_FATAL 吗?我问的原因是你永远不会在updateXMLFile() 函数的范围内定义$cdata...

标签: php xml cdata


【解决方案1】:

试穿它的大小。如果您对此有任何问题/对此有任何疑问,请告诉我(FIXED)。

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);

}

注意现在,如果 DOMDocument::load() 失败,这将引发异常 - 不要忘记 catch 它!

【讨论】:

    猜你喜欢
    • 2014-08-03
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 2023-03-08
    • 2011-08-11
    • 1970-01-01
    相关资源
    最近更新 更多