我有这个代码来创建和更新 xml 文件:
<?php
$xmlFile = 'config.xml';
$xml = new SimpleXmlElement('<site/>');
$xml->title = 'Site Title';
$xml->title->addAttribute('lang', 'en');
$xml->saveXML($xmlFile);
?>
这会生成以下 xml 文件:
<?xml version="1.0"?>
<site>
<title lang="en">Site Title</title>
</site>
问题是:有没有办法用这种方法/技术添加CDATA来创建下面的xml代码?
<?xml version="1.0"?>
<site>
<title lang="en"><![CDATA[Site Title]]></title>
</site>
<?php
// http://coffeerings.posterous.com/php-simplexml-and-cdata
// https://web.archive.org/web/20110223233311/http://coffeerings.posterous.com/php-simplexml-and-cdata
// Customized 'SimpleXMLElement' class.
class SimpleXMLExtended extends SimpleXMLElement {
public function addCData( $cdata_text ) {
$node = dom_import_simplexml( $this );
$ownerDocumentNode = $node->ownerDocument;
$node->appendChild( $ownerDocumentNode->createCDATASection( $cdata_text ));
}
}
// How to create the following example, below:
// <?xml version="1.0"?>
// <site>
// <title lang="en"><![CDATA[Site Title]]></title>
// </site>
/*
* Instead of SimpleXMLElement:
* $xml = new SimpleXMLElement( '<site/>' );
* create from custom class, in this case, SimpleXMLExtended.
*/
// '<site></site>'.
$xml = new SimpleXMLExtended( '<site/>' );
// '<title></title>'.
$xml->title = NULL; // VERY IMPORTANT! We need a node where to append.
$xml->title->addCData( 'Site Title' );
$xml->title->addAttribute( 'lang', 'en' );
// Save.
$xml->saveXML( $xmlFile );
?>
生成的 XML 文件:
<?xml version="1.0"?>
<site>
<title lang="en"><![CDATA[Site Title]]></title>
</site>
谢谢你佩塔
这是我的此类版本,它有一个快速 addChildWithCDATA 方法,基于 你的答案:
Class SimpleXMLElementExtended extends SimpleXMLElement {
/**
* Adds a child with $value inside CDATA
* @param unknown $name
* @param unknown $value
*/
public function addChildWithCDATA($name, $value = NULL) {
$new_child = $this->addChild($name);
if ($new_child !== NULL) {
$node = dom_import_simplexml($new_child);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection($value));
}
return $new_child;
}
}
就像这样使用它:
$node = new SimpleXMLElementExtended();
$node->addChildWithCDATA('title', 'Text that can contain any unsafe XML charachters like & and <>');
如果您不想扩展 SimpleXMLElement,您还可以为此创建一个辅助函数:
/**
* Adds a CDATA property to an XML document.
*
* @param string $name
* Name of property that should contain CDATA.
* @param string $value
* Value that should be inserted into a CDATA child.
* @param object $parent
* Element that the CDATA child should be attached too.
*/
$add_cdata = function($name, $value, &$parent) {
$child = $parent->addChild($name);
if ($child !== NULL) {
$child_node = dom_import_simplexml($child);
$child_owner = $child_node->ownerDocument;
$child_node->appendChild($child_owner->createCDATASection($value));
}
return $child;
};
class MySimpleXMLElement extends SimpleXMLElement{
public function addChildWithCData($name , $value) {
$new = parent::addChild($name);
$base = dom_import_simplexml($new);
$docOwner = $base->ownerDocument;
$base->appendChild($docOwner->createCDATASection($value));
}
}
$simpleXmlElemntObj = new MySimpleXMLElement('<site/>');
/* USAGE */
/* Standard */
$simpleXmlElemntObj->addChild('Postcode','1111');
/* With CDATA */
$simpleXmlElemntObj->addChildWithCData('State','Processing');
/* RESULT */
/*
<?xml version="1.0"?>
<site>
<Postcode>1111</Postcode>
<State><![CDATA[Processing]]></State>
</site>
*/
这是我的组合解决方案,使用 CDATA 添加子级或将 CDATA 添加到节点。
class SimpleXMLElementExtended extends SimpleXMLElement
{
/**
* Add value as CData to a given XML node
*
* @param SimpleXMLElement $node SimpleXMLElement object representing the child XML node
* @param string $value A text to add as CData
* @return void
*/
private function addCDataToNode(SimpleXMLElement $node, $value = '')
{
if ($domElement = dom_import_simplexml($node))
{
$domOwner = $domElement->ownerDocument;
$domElement->appendChild($domOwner->createCDATASection("{$value}"));
}
}
/**
* Add child node with value as CData
*
* @param string $name The child XML node name to add
* @param string $value A text to add as CData
* @return SimpleXMLElement
*/
public function addChildWithCData($name = '', $value = '')
{
$newChild = parent::addChild($name);
if ($value) $this->addCDataToNode($newChild, "{$value}");
return $newChild;
}
/**
* Add value as CData to the current XML node
*
* @param string $value A text to add as CData
* @return void
*/
public function addCData($value = '')
{
$this->addCDataToNode($this, "{$value}");
}
}
// Usage example:
$xml_doc = '<?xml version="1.0" encoding="utf-8"?>
<offers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1">
</offers>';
$xml = new SimpleXMLElementExtended($xml_doc);
$offer = $xml->addChild('o');
$offer->addAttribute('id', $product->product_id);
$offer->addAttribute('url', 'some url');
$cat = $offer->addChildWithCData('cat', 'Category description as CDATA');
// or
$cat = $offer->addChild('cat');
$cat->addCData('Category description as CDATA');