moccalotto / exemel
该软件包最新版本(0.4.1)没有可用的许可信息。
简化XML文档的读写
0.4.1
2016-07-30 09:54 UTC
Requires
- php: >=5.5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^1.11
- phpspec/phpspec: ^2.5
This package is not auto-updated.
Last update: 2024-09-14 19:25:38 UTC
README
使用fluint API轻松构建和修改XML文档。
安装
要将此软件包作为本地、项目依赖项添加到项目中,只需将 moccalotto/exemel
添加到项目中的 composer.json
文件中,如下所示
{ "require": { "moccalotto/exemel": "~0.1" } }
或者简单地调用 composer require moccalotto/exemel
演示
<?php require 'vendor/autoload.php'; function ensure($condition) { if (!$condition) { throw new RuntimeException('Condition failed'); } } $xml = new Moccalotto\Exemel\Xml(new SimpleXmlElement('<root/>')); // <root /> /* * Add attr="attr0" to the root element */ $xml->set('[attr]', 'attr0'); ensure($xml->get('[attr]') == 'attr0'); ensure($xml->root()['attr'] == 'attr0'); // <root attr="attr0" /> /* * Add an element called <foo> * Inside that, add an element called <bar> * Set the contents of <bar> to "el1" */ $xml->set('foo/bar', 'el1'); ensure($xml->get('foo/bar') == 'el1'); ensure((string) $xml->root()->foo->bar == 'el1'); // <root attr="attr0"> // <foo> // <bar>el1</bar> // </foo> // </root> /* * Add attr="attr1" to the newly added <bar> element */ $xml->set('foo/bar[ding]', 'attr1'); ensure($xml->get('foo/bar[ding]') == 'attr1'); ensure((string) $xml->root()->foo->bar['ding'] == 'attr1'); // <root attr="attr0"> // <foo> // <bar ding="attr1">el1</bar> // </foo> // </root> /* * Add a new <foo> element to the root. * Inside that, add a <bar> element with the contents "el2" */ $xml->set('foo[]/bar', 'el2'); ensure($xml->get('foo[1]/bar') == 'el2'); ensure((string) $xml->root()->foo[1]->bar == 'el2'); // <root attr="attr0"> // <foo> // <bar ding="attr1">el1</bar> // </foo> // <foo> // <bar>el2</bar> // </foo> // </root> echo $xml->formatted();