fauno/simple-xml-extended

SimpleXMLElement 扩展类

1.1.2 2020-02-20 10:31 UTC

This package is auto-updated.

Last update: 2024-09-21 17:47:02 UTC


README

GitHub license GitHub release Packagist Packagist Downloads GitHub issues GitHub forks GitHub stars PHP

SimpleXMLElement 扩展类 SimpleXMLExtended 增加了一个新的方法用于创建 CData 节点。还增加了一个新的方法用于输出格式优美的 XML。

要求

  • libxml PHP 扩展(默认启用)。
  • SimpleXML PHP 扩展(默认启用)。
  • DOM PHP 扩展(默认启用)。

安装

您可以使用 Composer 将此插件安装到您的应用中。

  composer require fawno/simple-xml-extended

或者,克隆/下载此仓库,并在您的项目中包含 src/SimpleXMLExtended.php 文件。

用法

  use Fawno\SimpleXMLExtended\SimpleXMLExtended;

  // Get a SimpleXMLExtended object from a DOM node
  $xml = simplexml_import_dom($dom, SimpleXMLExtended::class);

  // Interprets an XML file into an SimpleXMLExtended object
  $xml = simplexml_load_file($xml_file, SimpleXMLExtended::class);

  // Interprets a string of XML into an SimpleXMLExtended object
  $xml = simplexml_load_string($xml_string, SimpleXMLExtended::class);

  // Creates a new SimpleXMLExtended object
  $xml = new SimpleXMLExtended($xml_string);

  // Adds a child element to the XML node as cdata
  $xml->addChildCData('node_cdata', 'data as cdata');

  // Return a well-formed and nice formated XML string based on SimpleXMLExtended element
  $xml->formatXML()

示例

  require 'src/SimpleXMLExtended.php';

  use Fawno\SimpleXMLExtended\SimpleXMLExtended;

  $root = new SimpleXMLExtended('<?xml version="1.0" encoding="UTF-8"?><root/>');

  $root->addChildCData('node_cdata', 'data as cdata');

  print_r($root->asXML());
  /*
    Output:
      <?xml version="1.0" encoding="UTF-8"?>
      <root><node_cdata><![CDATA[data as cdata]]></node_cdata></root>
  */

  print_r($root->formatXML());
  /*
    Output:
      <?xml version="1.0" encoding="UTF-8"?>
      <root>
        <node_cdata><![CDATA[data as cdata]]></node_cdata>
      </root>
  */