|Last Updated||Ratings||Unique User Downloads||Download Rankings|
|2017-12-06 (6 days ago)||Not enough user ratings||Total: 71 This week: 13||All time: 8,966 This week: 73|
|xmtk-parser 0.2||MIT/X Consortium ...||5.6||XML, PHP 5|
This class can restructure the XML parsing results conveniently.
2017-09-17. Breaking change: the
xml_parse_into_array() method of the
has been renamed to
xmlParseIntoArray() according to PHP-FIG's PSR.
2017-09-30. Introduced the
Xmtk\Writer class to write XML from arrays.
Xmtk is a wrapper around the
It parses XML strings into the structs using the above function,
but then transforms its result into easy-to-handle array hierarchies.
Please, see example 1 to get the difference.
\Xmtk\Writer class writes hierarchical arrays to XML files.
In other words
Writer aimed to do the reverse work of
See example 2.
There is the
\Xmtk\CData class in the package.
It offers the
encode() method to convert strings to CData forms.
See the 3rd example to learn why this class useful.
This example shows the difference between the result.
Look at the listing 1. It is the input XML.
Listing 2 shows what result the
xml_parse_into_struct() will return.
And, finally, listing 3 is the result of
\Xmtk\Writer will convert the arrays looking like in the listing 3 below
into XML string like shown in the listing 1.
Let's suppose you need to put HTML markup into the some tag of XML. The next code will not work correctly.
$writer = new \Xmtk\Writer; $xml = $writer->xmlWriteFromArray( ['markup'=>'<strong>text</strong>']);
The value of the
$xml variable in this example will be:
This is wrong. To fix this situation you may use the
class next way.
$cdata = new \Xmtk\CData; $xml = $writer->xmlWriteFromArray( ['markup'=>$cdata->encode('<strong>text</strong>')]);
The output XML will look like the next one.
<bike> <wheel>front</wheel> <wheel>rear</wheel> <chain> <length>1</length> </chain> </bike>
Array (  => Array ( [tag] => bike [type] => open [level] => 1 [value] => )  => Array ( [tag] => wheel [type] => complete [level] => 2 [value] => front )  => Array ( [tag] => bike [value] => [type] => cdata [level] => 1 )  => Array ( [tag] => wheel [type] => complete [level] => 2 [value] => rear )  => Array ( [tag] => bike [value] => [type] => cdata [level] => 1 )  => Array ( [tag] => chain [type] => open [level] => 2 [value] => )  => Array ( [tag] => length [type] => complete [level] => 3 [value] => 1 )  => Array ( [tag] => chain [value] => [type] => cdata [level] => 2 )  => Array ( [tag] => chain [type] => close [level] => 2 )  => Array ( [tag] => bike [value] => [type] => cdata [level] => 1 )  => Array ( [tag] => bike [type] => close [level] => 1 ) )
Array ( [bike] => Array ( [wheel] => Array (  => front  => rear ) [chain] => Array ( [length] => 1 ) ) )
Xmtk stands for eXtensible Markup Tool-Kit.
Thanks for your interest.
In November, 2017 the project has been migrated to PHP 7.0. This migration was intended to avoid usage of legacy PHPUnit.
composer require xmtk/xmtk
#!/usr/local/bin/php <?php require_once __DIR__.'/vendor/autoload.php'; $parser = new \Xmtk\Parser; $xml = '<bike> <wheels> <wheel>The front one</wheel> <wheel>The rear one</wheel> </wheels> <chain> <count>191</count> </chain> </bike>'; $result = $parser->xmlParseIntoArray($xml); print_r($result); ?>
Array ( [bike] => Array ( [wheels] => Array ( [wheel] => Array (  => The front one  => The rear one ) ) [chain] => Array ( [count] => 191 ) ) )
\Xmtk\Writer class accepts arrays in the same format as the
See below for the usage example.
require_once __DIR__.'/../vendor/autoload.php'; $writer = new \Xmtk\Writer; $array = ['collection' => ['item' => ['salad', 'napkins', 'insecticide']]]; $xml = $writer->xmlWriteFromArray($array); print_r($xml); /* // Output. <?xml version='1.0'?> <collection> <item>salad</item> <item>napkins</item> <item>insecticide</item> </collection> */
\Xmtk\Parser treats repeating tags placed on the same level as arrays.
This means that if you have two or more
<bar/> tags inside the
then the array for the
foo tag will have child array
bar indexed by numbers.
Look at the
<wheel/> tags in the listing 1 and how they were processed by
\Xmtk\Parser in the listing 3.
All we love XML attributes.
But support of this important XML specification part is not implemented yet.
Contribute if you see that
Xmtk is cool!
Composer is used as an application level package manager. You may want to install Composer to your system for more convenient development process. For FreeBSD the command to setup Composer system-widely will look like the next one.
sudo pkg install php-composer
To run test you can use
make. There are several targets in the
parser-testsis to run only unit tests to test the
writer-testsis to run only unit tests to test the
testsis to run both the
make tests make parser-tests
|Version Control||Unique User Downloads||Download Rankings|