CakePHP
  • 文档
    • 手册
    • API
    • 视频
    • 报告安全问题
    • 隐私政策
    • 标识和商标
  • 商业解决方案
  • 周边产品
  • 公路旅行
  • 团队
  • 社区
    • 社区
    • 参与
    • 问题 (Github)
    • 烘焙坊
    • 精选资源
    • 培训
    • 聚会
    • 我的 CakePHP
    • CakeFest
    • 通讯
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • 帮助和支持
    • 论坛
    • Stack Overflow
    • IRC
    • Slack
    • 付费支持
CakePHP

C CakePHP 5.1 Chiffon API

  • 项目
    • CakePHP
      • CakePHP
      • Chronos
      • Elastic Search
      • 队列
  • 版本
    • 5.1
      • 5.1
      • 5.0
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

命名空间

  • 全局
  • Cake
    • Cache
    • Collection
    • Command
    • Console
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
    • I18n
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
      • Crypto
      • Exception
    • Validation
    • View

Xml 类

CakePHP 的 XML 处理。

这些类中的方法使使用 XML 的数据源能够正常工作。

命名空间: Cake\Utility

方法汇总

  • _createChild() protected static

    _fromArray() 的辅助函数。它将创建数组的子节点。

  • _fromArray() protected static

    递归方法,用于从数组创建子节点。

  • _loadXml() protected static

    解析输入数据并创建 SimpleXmlElement 对象或 DOMDocument 对象。

  • _toArray() protected static

    递归方法,用于转换为数组。

  • build() public static

    从给定的 XML 字符串、文件路径、URL 或数组初始化 SimpleXMLElement 或 DOMDocument。

  • fromArray() public static

    将数组转换为 SimpleXMLElement。

  • load() protected static

    解析输入数据并创建 SimpleXmlElement 对象或 DOMDocument 对象。

  • loadHtml() public static

    解析输入的 html 字符串并创建 SimpleXmlElement 对象或 DOMDocument 对象。

  • toArray() public static

    将此 XML 结构作为数组返回。

方法详情

_createChild() ¶ protected static

_createChild(array<string, mixed> $data): void

_fromArray() 的辅助函数。它将创建数组的子节点。

参数
array<string, mixed> $data

包含创建子节点信息的数组

返回
void

_fromArray() ¶ protected static

_fromArray(DOMDocument $dom, DOMDocumentDOMElement $node, mixed $data, string $format): void

递归方法,用于从数组创建子节点。

参数
DOMDocument $dom

DOMDocument 处理程序

DOMDocumentDOMElement $node

DOMElement 处理程序(子节点)

mixed $data

要附加到 $node 的数据数组。

string $format

可以是 'attributes' 或 'tags'。它决定嵌套键的放置位置。

返回
void
抛出
Cake\Utility\Exception\XmlException

_loadXml() ¶ protected static

_loadXml(string $input, array<string, mixed> $options): SimpleXMLElementDOMDocument

解析输入数据并创建 SimpleXmlElement 对象或 DOMDocument 对象。

参数
string $input

要加载的输入。

array<string, mixed> $options

要使用的选项。参见 Xml::build()

返回
SimpleXMLElementDOMDocument
抛出
Cake\Utility\Exception\XmlException

_toArray() ¶ protected static

_toArray(SimpleXMLElement $xml, array<string, mixed> $parentData, string $ns, list<string> $namespaces): void

递归方法,用于转换为数组。

参数
SimpleXMLElement $xml

SimpleXMLElement 对象

array<string, mixed> $parentData

包含数据的父级数组

string $ns

当前子节点的命名空间

list<string> $namespaces

XML 中的命名空间列表

返回
void

build() ¶ public static

build(object|array|string $input, array<string, mixed> $options = []): SimpleXMLElementDOMDocument

从给定的 XML 字符串、文件路径、URL 或数组初始化 SimpleXMLElement 或 DOMDocument。

用法

从字符串构建 XML

$xml = Xml::build('<example>text</example>');

从字符串构建 XML(输出 DOMDocument)

$xml = Xml::build('<example>text</example>', ['return' => 'domdocument']);

从文件路径构建 XML

$xml = Xml::build('/path/to/an/xml/file.xml', ['readFile' => true]);

从远程 URL 构建 XML

use Cake\Http\Client;

$http = new Client();
$response = $http->get('http://example.com/example.xml');
$xml = Xml::build($response->body());

从数组构建

 $value = [
     'tags' => [
         'tag' => [
             [
                 'id' => '1',
                 'name' => 'defect'
             ],
             [
                 'id' => '2',
                 'name' => 'enhancement'
             ]
         ]
     ]
 ];
$xml = Xml::build($value);

从数组构建 XML 时,请确保只有一个顶级元素。

选项

  • return 可以是 'simplexml' 以返回 SimpleXMLElement 对象,也可以是 'domdocument' 以返回 DOMDocument 对象。
  • loadEntities 默认值为 false。设置为 true 以启用加载 <!ENTITY 定义。出于安全原因,默认情况下禁用此功能。
  • readFile 设置为 true 以启用文件读取。默认情况下禁用此功能以防止本地文件系统访问。仅当输入安全时才启用此设置。
  • parseHuge 启用 LIBXML_PARSEHUGE 标志。

如果使用数组作为输入,则可以传递 Xml::fromArray 中的 options。

参数
object|array|string $input

XML 字符串、文件路径、URL 或数组

array<string, mixed> $options optional

要使用的选项

返回
SimpleXMLElementDOMDocument
抛出
Cake\Utility\Exception\XmlException

fromArray() ¶ public static

fromArray(object|array $input, array<string, mixed> $options = []): SimpleXMLElementDOMDocument

将数组转换为 SimpleXMLElement。

选项

  • format 如果创建子节点 ('tags') 或属性 ('attributes')。
  • pretty 当设置为 true 时,返回格式化的 Xml。默认为 false
  • version XML 文档的版本。默认值为 1.0。
  • encoding XML 文档的编码。如果为 null,则从 XML 头中删除。默认为应用程序的编码。
  • return 如果返回 SimpleXMLElement 对象 ('simplexml') 或 DOMDocument 对象 ('domdocument')。默认为 SimpleXMLElement。

使用以下数据

$value = [
   'root' => [
       'tag' => [
           'id' => 1,
           'value' => 'defect',
           '@' => 'description'
        ]
    ]
];

调用 Xml::fromArray($value, 'tags'); 将生成

<root><tag><id>1</id><value>defect</value>description</tag></root>

调用 Xml::fromArray($value, 'attributes'); 将生成

<root><tag id="1" value="defect">description</tag></root>

参数
object|array $input

包含数据的数组或集合实例。

array<string, mixed> $options optional

要使用的选项。

返回
SimpleXMLElementDOMDocument
抛出
Cake\Utility\Exception\XmlException

load() ¶ protected static

load(string $input, array<string, mixed> $options, Closure $callable): SimpleXMLElementDOMDocument

解析输入数据并创建 SimpleXmlElement 对象或 DOMDocument 对象。

参数
string $input

要加载的输入。

array<string, mixed> $options

要使用的选项。参见 Xml::build()

Closure $callable

应该返回 SimpleXMLElement 或 DOMDocument 实例的闭包。

返回
SimpleXMLElementDOMDocument
抛出
Cake\Utility\Exception\XmlException

loadHtml() ¶ public static

loadHtml(string $input, array<string, mixed> $options = []): SimpleXMLElementDOMDocument

解析输入的 html 字符串并创建 SimpleXmlElement 对象或 DOMDocument 对象。

参数
string $input

要加载的输入 html 字符串。

array<string, mixed> $options optional

要使用的选项。参见 Xml::build()

返回
SimpleXMLElementDOMDocument
抛出
Cake\Utility\Exception\XmlException

toArray() ¶ public static

toArray(SimpleXMLElementDOMNode $obj): array

将此 XML 结构作为数组返回。

参数
SimpleXMLElementDOMNode $obj

SimpleXMLElement、DOMNode 实例

返回
数组
抛出
Cake\Utility\Exception\XmlException
OpenHub
Pingping
Linode
  • 商业解决方案
  • 展示
  • 文档
  • 手册
  • API
  • 视频
  • 报告安全问题
  • 隐私政策
  • 标识和商标
  • 社区
  • 参与
  • 问题 (Github)
  • 烘焙坊
  • 精选资源
  • 培训
  • 聚会
  • 我的 CakePHP
  • CakeFest
  • 通讯
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • 帮助和支持
  • 论坛
  • Stack Overflow
  • IRC
  • Slack
  • 付费支持

使用 CakePHP API 文档 生成