PHP中的XML编码/解码类


XML encode/decode class in PHP

有人能推荐一个好的、稳定的类来用PHP编码和解码XML吗?

编辑

在php.net上找到了这个例子,但无法使其工作。。未返回错误消息

$sxe = new SimpleXMLElement();
$sxe->addAttribute('type', 'documentary');
$movie = $sxe->addChild('movie');
$movie->addChild('title', 'PHP2: More Parser Stories');
$movie->addChild('plot', 'This is all about the people who make it work.');
$characters = $movie->addChild('characters');
$character  = $characters->addChild('character');
$character->addChild('name', 'Mr. Parser');
$character->addChild('actor', 'John Doe');
$rating = $movie->addChild('rating', '5');
$rating->addAttribute('type', 'stars');
echo $sxe->asXML();

SimpleXML可能是最简单的。示例:

<root>
   <node>
      <sub>Text</sub>
   </node>
</root>

 

$xml = new SimpleXMLElement('xml_file.xml', 0, true);
echo $xml->node->sub; // Displays "Text"

编辑:

为了响应不起作用的代码,您需要在类的初始化中包含一个根节点:

$sxe = new SimpleXMLElement('<root />');

编码/解码是指写/读吗?XML不是代码,只是标记。不过,XML有一种叫做字符/实体编码的东西。

PHP基本上有两个类或库可以使用。SimpleXML和DOMElement。您必须检查phpinfo以查看它们是否已启用,并检查php.ini以启用它们。