stanclik / to-xml
A simple library to generate xml files
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:libary
Requires
- php: >=7.4.0
This package is auto-updated.
Last update: 2024-09-10 13:25:07 UTC
README
An easy-to-use library for generating XML files.
Installation
Install usign composer
composer require stanclik/toxml
Usage/Examples
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();
Result:
<?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
Blocks::header([ 'version' => '1.0' // add header on start of the content ])
Tag
Blocks::tag( 'tag-name' // tag name ex: <tag-name> [ // nested tags ], [ 'param' => 'foo' // params ex: <tag-name params="foo"> ] )
Content
Blocks::content( 'Lorem Ipsum' // content inside the element, true|false // wrap the content in CDATA tags )
Raw
Blocks::raw( '<tag><!CDATA[some stuff]]>' // just add raw tag )