English 中文(简体)
• 在我的XML中增加CDATA,使用简单的xml和php
原标题:Can t add CDATA into my XML string using simplexml with php
  • 时间:2012-05-04 08:54:25
  •  标签:
  • php
  • xml
  • cdata

I m simply wanting to add cdata to an xml node - description. My xml function is below. I have tried using bits of the following function on php.net in my function

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

“当值xml文件树,只是想到描述标语中的元数据”/

最佳回答

如此之大。 请允许我了解一下,你是否与它有任何问题(<>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);

}

N.B. 如今,这 throw了一种例外情况::load(<>>/code>失败——不忘记fish!

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签