PHP Classes
elePHPant
Icontem

\Xmtk\Parser: Restructure the XML parsing results conveniently

Recommend this page to a friend!
  Info   View files View files (10)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-12-06 (6 days ago) RSS 2.0 feedNot enough user ratingsTotal: 71 This week: 13All time: 8,966 This week: 73Up
Version License PHP version Categories
xmtk-parser 0.2MIT/X Consortium ...5.6XML, PHP 5
Description Author

This class can restructure the XML parsing results conveniently.

It can transform results of the xml_parse_into_struct() function into form, more convenient to understand and to work with.

Innovation Award
PHP Programming Innovation award nominee
September 2017
Number 12
This class can restructure the XML parsing results conviniently.

It can transform results of the xml_parse_into_struct() function into form, more convenient to understand and to work with.

Manuel Lemos
Name: Vasily Blinkov <contact>
Classes: 1 package by
Country: Russian Federation Russian Federation
Age: 27
All time rank: 4126112 in Russian Federation Russian Federation
Week rank: 55 Up5 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 1x

Details

CHANGELOG

2017-12-02. Add the \Xmtk\CData class. 2017-11-26. The unit testing using GitLab CI and PHPUnit was implemented.

2017-09-17. Breaking change: the xml_parse_into_array() method of the Parser class has been renamed to xmlParseIntoArray() according to PHP-FIG's PSR.

2017-09-30. Introduced the Xmtk\Writer class to write XML from arrays.

ABOUT

What Xmtk is?

xml parser

Xmtk is a wrapper around the xml_parse_into_struct() function. 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.

xml writer

The \Xmtk\Writer class writes hierarchical arrays to XML files. In other words Writer aimed to do the reverse work of Parser. See example 2.

cdata

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.

example 1

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\Parser work.

example 2

\Xmtk\Writer will convert the arrays looking like in the listing 3 below into XML string like shown in the listing 1.

example 3

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: <markup><strong>text</strong></markup>. This is wrong. To fix this situation you may use the \Xmtk\CData 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.

<markup><![CDATA[<strong>text</strong>]]></markup>

listing 1 (input xml)

<bike>
	<wheel>front</wheel>
	<wheel>rear</wheel>
	<chain>
		<length>1</length>
	</chain>
</bike>

listing 2 (php function)

Array
(
    [0] => Array
        (
            [tag] => bike
            [type] => open
            [level] => 1
            [value] => 
	
        )

    [1] => Array
        (
            [tag] => wheel
            [type] => complete
            [level] => 2
            [value] => front
        )

    [2] => Array
        (
            [tag] => bike
            [value] => 
	
            [type] => cdata
            [level] => 1
        )

    [3] => Array
        (
            [tag] => wheel
            [type] => complete
            [level] => 2
            [value] => rear
        )

    [4] => Array
        (
            [tag] => bike
            [value] => 
	
            [type] => cdata
            [level] => 1
        )

    [5] => Array
        (
            [tag] => chain
            [type] => open
            [level] => 2
            [value] => 
		
        )

    [6] => Array
        (
            [tag] => length
            [type] => complete
            [level] => 3
            [value] => 1
        )

    [7] => Array
        (
            [tag] => chain
            [value] => 
	
            [type] => cdata
            [level] => 2
        )

    [8] => Array
        (
            [tag] => chain
            [type] => close
            [level] => 2
        )

    [9] => Array
        (
            [tag] => bike
            [value] => 

            [type] => cdata
            [level] => 1
        )

    [10] => Array
        (
            [tag] => bike
            [type] => close
            [level] => 1
        )

)

listing 3 (parser wrapper)

Array
(
    [bike] => Array
        (
            [wheel] => Array
                (
                    [0] => front
                    [1] => rear
                )

            [chain] => Array
                (
                    [length] => 1
                )

        )

)

What Xmtk stands for?

Xmtk stands for eXtensible Markup Tool-Kit. Thanks for your interest.

USAGE

Requirements

In November, 2017 the project has been migrated to PHP 7.0. This migration was intended to avoid usage of legacy PHPUnit.

Setup

composer require xmtk/xmtk

\Xmtk\Parser

php

#!/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);

?>

output

Array
(
    [bike] => Array
        (
            [wheels] => Array
                (
                    [wheel] => Array
                        (
                            [0] => The front one
                            [1] => The rear one
                        )

                )

            [chain] => Array
                (
                    [count] => 191
                )

        )

)

\Xmtk\Writer

The \Xmtk\Writer class accepts arrays in the same format as the \Xmtk\Parser returns. 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>
*/

IMPORTANT NOTES

arrays

The \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 <foo/> node, 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 the \Xmtk\Parser in the listing 3.

KNOWN ISSUES

attributes

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!

DEVELOPMENT

Tools

  • PL: Xmtk written in the PHP7 programming language. You may need to install the next packages (FreeBSD 10.x): php70, php70-filter, php70-hash, php70-iconv, php70-json, php70-openssl, php70-phar, php70-xml, php70-zlib.
  • IDE: Not used (no projects like nbproject/).
  • UT: PHPUnit is the unit-testing framework used. Install it according to the official manual.

composer

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

tests

This package uses PHPUnit for unit testing. PHPUnit should be installed to the $PATH.

To run test you can use make. There are several targets in the Makefile.

  1. parser-tests is to run only unit tests to test the Parser class.
  2. writer-tests is to run only unit tests to test the Writer class.
  3. tests is to run both the parser-tests and the writer-tests targets.

EXAMPLES

make tests
make parser-tests

  Files folder image Files  
File Role Description
Files folder imagesrc (3 files)
Files folder imagetests (3 files)
Accessible without login Plain text file composer.json Data Composer package configuration.
Accessible without login Plain text file composer.lock Data Composer dependencies lock file.
Accessible without login Plain text file Makefile Data Auxiliary data
Accessible without login Plain text file README.md Doc. README file. Documentation.

  Files folder image Files  /  src  
File Role Description
  Plain text file CData.php Class Class source
  Plain text file Parser.php Class The class.
  Plain text file Writer.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file CDataTests.php Class Class source
  Plain text file ParserTests.php Class Class source
  Plain text file WriterTests.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:71
This week:13
All time:8,966
This week:73Up