8fold/php-xml-builder 问题修复 & 功能扩展

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

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

8fold/php-xml-builder

最新稳定版本:2.0.0

Composer 安装命令:

composer require 8fold/php-xml-builder

包简介

A library for building XML document and element strings.

README 文档

README

XML Builder is designed to build a string, not a document object model (DOM) or abstract syntax tree (AST).

For DOM or AST, there are other libraries and native implementations (ex. PHP:DOM and SimpleXML).

You might use XML Builder to generate a string you feed into either PHP:DOM or Simple XML. Or, use the string as the body of an HTTP response.

Use the Element class to create individual nodes within the document.

Use the Document class to generate the doctype declaration and root level element.

Installation

composer require 8fold/php-xml-builder

Usage

Warning: Users of this library are responsible for sanitizing content.

use Eightfold\XMLBuilder\Document;
use Eightfold\XMLBuilder\Element;
use Eightfold\XMLBuilder\Cdata;

echo Document::create('root',
  Element::create('child',
    Element::create('grandchild')->omitEndTag()->props('name Xavier'),
    Cdata::create('Hello, my name is Xavier!')
  )
);

Output:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<root><child><grandchild name="Xavier"/><![CDATA[Hello, my name is Xavier!]]></child></root>

Output (formatted):

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<root>
  <child>
    <grandchild name="Xavier"/>
    <![CDATA[Hello, my name is Xavier!]]>
  </child>
</root>

Alternatively, there is a shorthand variation.

The shorthand method uses the __callStatic PHP magic method.

use Eightfold\XMLBuilder\Document;
use Eightfold\XMLBuilder\Element;
use Eightfold\XMLBuilder\Cdata;

echo Document::root(
  Element::child(
    Element::grandchild()->omitEndTag()->props('name Xavier'),
    Cdata::create('Hello, my name is Xavier!')
  )
);

// output: Same as previous example.

Comments are available:

use Eightfold\XMLBuilder\Document;
use Eightfold\XMLBuilder\Element;
use Eightfold\XMLBuilder\Cdata;

echo Document::create(
  'root',
  Comment::create('comment'),
  Element::create('tag')->omitEndTag()
);

Output (unformatted):

<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>
<root>
<!-- comment -->
<tag /></root>

Compatibility

XML Builder version PHP version
1+ 8.0+
0+ 7.4+

Details

The origins of this library was an experiment where PHP was viewed as a "pure" programming language, not a template engine that grew into being a language.

The primary pain point was to maintain the feel of writing human-readable XML (tabs and spaces) while reducing the risk of human error; specifically, mismatched beginning and end tags.

The secondary pain point was that PHP:DOM and SimpleXML felt cumbersome when generating XML and HTML documents.

Other

统计信息

  • 总下载量: 11.94k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-10-04