伺服 / fluidxml
简洁流畅的XML操作库
2.0.0
2023-11-08 13:40 UTC
Requires
- php: ^8.1
- ext-dom: *
- ext-simplexml: *
Requires (Dev)
- apigen/apigen: 4.*
- peridot-php/peridot: 1.*
- peridot-php/peridot-code-coverage-reporters: 1.*
- rector/rector: ^0.18.6
README
变更日志
2.0.0 (2023-11-06):
PHP 8.1是新的最低要求版本。
...
FluidXML
FluidXML是一个PHP库,旨在使用简洁和流畅的API操作XML文档。
它利用流畅编程模式使其变得既有趣又有效。
$book = fluidxml(); $book->add('title', 'The Theory Of Everything') ->add('author', 'S. Hawking') ->add('chapters', true) ->add('chapter', 'Ideas About The Universe', ['id' => 1]) ->add('chapter', 'The Expanding Universe', ['id' => 2]);
或者,如果您更喜欢,还有扩展语法。
$book = new FluidXml(); $book->addChild('title', 'The Theory Of Everything') ->addChild('author', 'S. Hawking') ->addChild('chapters', true) ->addChild('chapter', 'Ideas About The Universe', ['id' => 1]) ->addChild('chapter', 'The Expanding Universe', ['id' => 2]);
使用FluidXML,DOM操作变得既快又清晰,又具有表现力。
PHP数组是第一等公民。
$book->add([ 'title' => 'The Theory Of Everything', 'author' => 'S. Hawking', 'chapters' => [ [ 'chapter' => [ '@id' => '1', '@' => 'Ideas About The Universe' ] ], [ 'chapter' => [ '@id' => '2', '@' => 'The Expanding Universe' ] ], ]]);
echo $book;
<?xml version="1.0" encoding="UTF-8"?> <doc> <title>The Theory Of Everything</title> <author>S. Hawking</author> <chapters> <chapter id="1">Ideas About The Universe</chapter> <chapter id="2">The Expanding Universe</chapter> </chapters> </doc>
XPath是王者。
$book->query('//title', '//author', '//chapter') ->attr('lang', 'en');
CSS选择器也很棒。
$book->query('#id', '.class1.class2', 'div p > span') ->attr('lang', 'en'); // Many other selectors are available.
XML/CSS命名空间得到全面覆盖。
$book->namespace('xhtml', 'http://www.w3.org/1999/xhtml') ->add('xhtml:h1') ->query('//xhtml:h1') // XPath namespace. ->query('xhtml|h1'); // CSS namespace.
有时,XML片段是最快的方法。
$book->add(<<<XML <cover class="front"> <img src="http://goo.gl/kO3Iov"/> </cover> <cover class="back"> <img src="http://goo.gl/kO3Iov"/> </cover> XML );
一切都是流畅的,甚至包括迭代。
$book->query('//chapter')->each(function ($i) { $this->attr('id', $i); });
$book->query('//chapters') ->times(3) ->add('chapter') ->times(4, function ($i) { $this->add('chapter'); $this->add('illustration'); });
您也可以轻松地映射节点。
$chaptersIds = $book->query('//chapter')->map(function ($i, $it) { return $it->getAttribute('id'); });
$book->query('//chapters') ->times(3) ->add('chapter') ->times(4, function ($i) { $this->add('chapter'); $this->add('illustration'); });
如果某些查询过于复杂,无法使用XPath/CSS表达,
过滤就是您的朋友。
$book->query('//chapters') ->filter(function ($i, $node) { return $i % 2 === 0; }) ->attr('even');
与现有的DOMDocument和SimpleXML的互操作性就像魔法一样简单。
直接导入它们或在FluidXML流程的任何位置注入它们。
fluidxml($domdocument) ->query('/html/body') ->add($simplexml); // Yes, we merged a DOMDocument with a SimpleXMLElement // and everything is still fluid.
不要害羞,说出来:“太棒了!” ^_^
还有许多其他API可用
__invoke()
append()
/appendSibling()
prepend()
/prependSibling()
addText()
text()
/setText()
addCdata()
cdata()
/setCdata()
addComment()
comment()
/setComment()
remove()
size()
/length()
load()
save()
dom()
xml()
html()
__toString()
array()
- ...
还有疑问?
FluidXML易于使用,简洁且有效。
如果还不够,它还有一个全面的测试套件,代码覆盖率100%。
但您自己尝试后会有最好的答案。
要求
- FluidXML v2(最新版)需要PHP >= 8.1
- FluidXML v1(遗留版本)需要PHP >=5.6 <8
安装
- 克隆仓库:
git clone https://github.com/servo-php/fluidxml.git
- 使用Composer:
composer require servo/fluidxml
入门
- 克隆仓库:
require_once 'FluidXml.php';
- 使用Composer:
require_once 'vendor/autoload.php';
按需使用类和函数。
use function \FluidXml\fluidxml; use function \FluidXml\fluidns; use function \FluidXml\fluidify;
use \FluidXml\FluidXml; use \FluidXml\FluidNamespace;
文档
还有很多其他示例
- 在
doc/Examples/
文件夹中 - 在
specs/FluidXml.php
文件中(作为测试用例)
它们涵盖了从最简单的情况到最复杂的场景。
查看API以发现所有可用的操作
并前往Wiki页面获取更多阅读资料。
捐赠
如果您认为这个代码很棒,或者您想表达
您深深的感激之情 ♥,请给我买杯咖啡。
路线图
- 扩展文档
- 扩展API
作者
丹尼尔·奥兰多 <fluidxml@danieleorlando.io>
许可证
FluidXML遵循BSD 2-Clause许可证。
详细信息请参阅doc/LICENSE.txt
。