servo/fluidxml 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

servo/fluidxml

Composer 安装命令:

composer require servo/fluidxml

包简介

Concise and fluent XML manipulation library

README 文档

README

Travis Build Coveralls Coverage Scrutinizer Quality Code Climate Quality

Packagist License Packagist Last Release Packagist Total Downloads

Changelog

2.0.0 (2023-11-06):

PHP 8.1 is the new minimum required version.

...

The full changes list.


Buy Me a Coffee at ko-fi.com

FluidXML

Servo-PHP Logo

      

FluidXML Logo

FluidXML is a PHP library designed to manipulate XML documents with a concise and fluent API.
It leverages the fluent programming pattern to be fun and effective.

$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]);

Or, if you prefer, there is an extended syntax.

$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]);

With FluidXML the DOM manipulation becomes fast, clear and expressive.

PHP Arrays are first class citizens.

$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 is king.

$book->query('//title', '//author', '//chapter')
        ->attr('lang', 'en');

And CSS Selectors rock.

$book->query('#id', '.class1.class2', 'div p > span')
        ->attr('lang', 'en');

// Many other selectors are available.

XML/CSS Namespaces are fully covered.

$book->namespace('xhtml', 'http://www.w3.org/1999/xhtml')
     ->add('xhtml:h1')
     ->query('//xhtml:h1')  // XPath namespace.
     ->query('xhtml|h1');   // CSS namespace.

And sometimes XML Fragments are the fastest way.

$book->add(<<<XML
    <cover class="front">
        <img src="http://goo.gl/kO3Iov"/>
    </cover>
    <cover class="back">
        <img src="http://goo.gl/kO3Iov"/>
    </cover>
XML
);

Everything is fluent, even iterations.

$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');
        });

You can map nodes easily too.

$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');
        });

Whether some queries are too complex to express with XPath/CSS,
filtering is your friend.

$book->query('//chapters')
        ->filter(function ($i, $node) {
            return $i % 2 === 0;
        })
        ->attr('even');

Interoperability with existing DOMDocument and SimpleXML is simply magic.
Import them or inject them in any point of the FluidXML flow just like that.

fluidxml($domdocument)
    ->query('/html/body')
         ->add($simplexml);

// Yes, we merged a DOMDocument with a SimpleXMLElement
// and everything is still fluid.

Don't be shy and tell it: « IT'S AWESOME! » ^_^

Many other APIs are available:

  • __invoke()
  • append()/appendSibling()
  • prepend()/prependSibling()
  • addText()
  • text()/setText()
  • addCdata()
  • cdata()/setCdata()
  • addComment()
  • comment()/setComment()
  • remove()
  • size()/length()
  • load()
  • save()
  • dom()
  • xml()
  • html()
  • __toString()
  • array()
  • ...

Still doubts?

FluidXML is fun to use, concise and effective.

If it's not enough, it has a comprehensive test suite with a 100% code coverage.

But you'll have the best answer trying it yourself.

100% Code Coverage

Requirements

  • FluidXML v2 (latest) requires PHP >= 8.1
  • FluidXML v1 (legacy) requires PHP >=5.6 <8

Installation

  • Cloning the repository:
git clone https://github.com/servo-php/fluidxml.git
  • Using Composer:
composer require servo/fluidxml

Getting Started

  • Cloning the repository:
require_once 'FluidXml.php';
  • Using Composer:
require_once 'vendor/autoload.php';

use classes and functions as you need.

use function \FluidXml\fluidxml;
use function \FluidXml\fluidns;
use function \FluidXml\fluidify;
use \FluidXml\FluidXml;
use \FluidXml\FluidNamespace;

See the documentation to get started and become a ninja.

Documentation

10 minutes reading
Follow the Getting Started tutorial to become a ninja in no time.

Many other examples are available:

All them cover from the simplest case to the most complex scenario.

Take a look at the APIs to discover all the available manipulation operations,
and go to the Wiki Page for more reading.

Donation

If you think this code is awesome or if you want to demonstrate
your immense gratitude , buy me a coffe.

Buy Me a Coffee at ko-fi.com

Roadmap

  • Extending the documentation
  • Expanding the APIs

Author

Daniele Orlando <fluidxml@danieleorlando.io>

License

FluidXML is licensed under the BSD 2-Clause License.

See doc/LICENSE.txt for the details.

servo/fluidxml 适用场景与选型建议

servo/fluidxml 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.24M 次下载、GitHub Stars 达 461, 最近一次更新时间为 2015 年 11 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「templating」 「xml」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 servo/fluidxml 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 servo/fluidxml 我们能提供哪些服务?
定制开发 / 二次开发

基于 servo/fluidxml 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.24M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 464
  • 点击次数: 20
  • 依赖项目数: 10
  • 推荐数: 0

GitHub 信息

  • Stars: 461
  • Watchers: 24
  • Forks: 46
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2015-11-19