PHP code example of lucid / xml

1. Go to this page and download the library: Download lucid/xml library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lucid / xml example snippets




use Lucid\Xml\Parser;

$parser = new Parser;

$parser->parse('<data><foo>bar</foo></data>');




use Lucid\Xml\Parser;

$parser = new Parser;

$parser->parse('/path/to/data.xml');




use Lucid\Xml\Parser;

$parser = new Parser;

$parser->parseDom($dom);




use Lucid\Xml\Parser;

$parser = new Parser;

$parser->parseDomElement($element);




use Lucid\Xml\Parser;

$xml = '<data><node id="1">some text</node></data>'

$parser = new Parser;
$parser->setAttributesKey('__attrs__');
$parser->parse($xml);

['data' => ['node' => ['__attrs__' => ['id' => 1], 'value' => 'some text']]];

$parser->setMergeAttributes(true);
$parser->parse($xml);

['data' => ['node' => ['id' => 1, 'value' => 'some text']]];



use Lucid\Xml\Parser;

$parser = new Parser;

$parser->setKeyNormalizer(function ($key) {
	// do string transfomations
	return $key;
});

$parser->parseDomElement($element);




use Lucid\Xml\Parser;

$parser = new Parser;

$parser->setIndexKey('item');




['entries' => ['entry' => [1, 2]]]




$parser->setPluralizer(function ($string) {
	if ('entry' === $string) {
		return 'entries';
	}
});



['entries' => [1, 2]]



use Lucid\Xml\Writer;

$writer = new Writer;

$data = [
	'foo' => 'bar'
];

$writer->dump($data); // <root><foo>bar</foo></root>

// set the xml root node name:

$writer->dump($data, 'data'); // <data><foo>bar</foo></data>





use Lucid\Xml\Writer;

$writer = new Writer;

$data = [
	'foo' => 'bar'
];

$dom = $writer->writeToDom($data);




use Lucid\Xml\Writer;
use Lucid\Xml\Normalizer\Normalizer;

$writer = new Writer(new Normalizer);

// or

$writer->setNormalizer($myNormalizer);



$writer->setInflector(function ($string) {
	if ('items' === $string) {
		return 'item';
	}
});



$writer->setEncoding($encoding); // string



$writer->setKeyMap([
	'nodeName' => ['id', 'entry'] // nested keys 'id' and 'entry' of the key
	element 'nodeName' will be set as attributes instead of childnodes.
]);




$data = [
	'foo' => [
		'@attributes' => [
			'bar' => 'baz'
		],
		'value' => 'tab'
	]
];



$writer->useKeyAsValue('value');

$writer->dump($data);

$data = ['data' => [1, 2, 3]];
$writer->dump($data);

$writer->useKeyAsIndex('thing');
$writer->dump($data);

$data = ['data' => [1 => 'foo', 4 => 'bar', 3 => 'baz']];
$writer->dump($data);