stanclik / to-xml
一个用于生成XML文件的简单库
dev-main
2024-02-10 12:14 UTC
Requires
- php: >=7.4.0
This package is auto-updated.
Last update: 2024-09-10 13:25:07 UTC
README
一个易于使用的生成XML文件的库。
安装
使用composer安装
composer require stanclik/toxml
用法/示例
require 'vendor/autoload.php'; use \Stanclik\ToXml\ToXml; use \Stanclik\ToXml\Blocks; $xml = new ToXml([]); // set document header $xml->setHeaders(Blocks::header(['version' => '1.0', 'encoding' => Blocks::UTF_8])); // set root element $xml->root('offers', ['param' => 'foo']); $xml->add( Blocks::tag('item', [ // set item Blocks::tag('name', [Blocks::content('Some item')]), Blocks::tag('description', [Blocks::content('Some description')]), Blocks::tag('attributes', [ // set nested elements Blocks::tag('some-attribute', [], ['params' => 'foo']), Blocks::tag('some-attribute', [], ['params' => 'foo']), Blocks::tag('some-attribute', [], ['params' => 'foo']), ]) ]) ); // call method to render xml; $xml->render(); // print xml as a string; header('Content-type: application/xml'); $xml->print(); // return xml as a string $xml->get();
结果
<?xml version="1.0" encoding="UTF-8"?> <offers param="foo"> <item> <name> <![CDATA[ Some item ]]> </name> <description> <![CDATA[ Some description ]]> </description> <attributes> <some-attribute params="foo"/> <some-attribute params="foo"/> <some-attribute params="foo"/> </attributes> </item> </offers>
块
标题
Blocks::header([ 'version' => '1.0' // add header on start of the content ])
标签
Blocks::tag( 'tag-name' // tag name ex: <tag-name> [ // nested tags ], [ 'param' => 'foo' // params ex: <tag-name params="foo"> ] )
内容
Blocks::content( 'Lorem Ipsum' // content inside the element, true|false // wrap the content in CDATA tags )
原始数据
Blocks::raw( '<tag><!CDATA[some stuff]]>' // just add raw tag )