2017 © Pedro Peláez
 

library easyxml

XML parser with a functional approach

image

mindplay/easyxml

XML parser with a functional approach

  • Wednesday, May 24, 2017
  • by mindplay.dk
  • Repository
  • 1 Watchers
  • 1 Stars
  • 2,163 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 4 Versions
  • 3 % Grown

The README.md

mindplay/easyxml

Build Status, (*1)

Scrutinizer Code Quality, (*2)

Code Coverage, (*3)

Functional XML-reader for PHP 5.3+., (*4)

A somewhat different approach to reading/parsing XML files with PHP, using a hierarchy of anonymous functions (closures) reflecting the hierarchy of the XML document itself., (*5)

This is useful when reading structured XML documents - e.g. XML documents with a predictable structure. It's probably less than enjoyable when reading unstructured documents, such as XHTML documents., (*6)

Parsing happens on-the-fly, e.g. avoiding the overhead of loading an entire document into memory and performing repetitive queries against it. This approach is memory efficient, enabling you to parse very large documents in a streaming fashion - it is not super fast (throughput ~500 KB/sec on my laptop) but XML parsing is never truly fast, so you should definitely always cache the parsed results., (*7)

Usage

Let's say you wish to read the following XML file:, (*8)

<?xml version="1.0" encoding="UTF-8"?>
<cats>
    <cat name="whiskers">
        <kitten name="mittens"/>
    </cat>
    <cat name="tinker">
        <kitten name="binky"/>
    </cat>
</cats>

Your reader might look something like this:, (*9)

$doc = new Parser();

$doc['cats/cat'] = function (Visitor $cat, $name) {
    echo "a cat named: {$name}\n";

    $cat['kitten'] = function ($name) {
        echo "a kitten named: {$name}\n";
    };
};

$doc->parseFile('my_cats.xml');

The output would be this:, (*10)

a cat named: whiskers
a kitten named: mittens
a cat named: tinker
a kitten named: binky

If it's not obvious, the path cats/cat designates a <cat> node inside a <cats> node., (*11)

You can also match text-nodes, e.g. a path like foo/bar#text will match YO in <foo><bar>YO</bar></foo>., (*12)

And finally, you can use #end to match closing tags, if needed., (*13)

Incidentally, I don't actually have cats - but if I did, you can bet those would be their names., (*14)

See "test.php" and "example/cd_catalog.php" for more examples of how to use this., (*15)

The Versions

24/05 2017

dev-feature/xmlns-support

dev-feature/xmlns-support

XML parser with a functional approach

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3.0

 

The Development Requires

14/05 2015

dev-master

9999999-dev

XML parser with a functional approach

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3.0

 

The Development Requires

13/01 2015

1.0.1

1.0.1.0

XML parser with a functional approach

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3.0

 

The Development Requires

10/11 2014

1.0.0

1.0.0.0

XML parser with a functional approach

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3.0

 

The Development Requires