Rand Stats

XML::Actions

cpan:MARTIMM

XML Actions on every node

Build Status AppVeyor Build Status License

Synopsis

use Test;
use XML::Actions;

my Str $file = "a.xml";
$file.IO.spurt(Q:q:to/EOXML/);
  <scxml xmlns="http://www.w3.org/2005/07/scxml"
         version="1.0"
         initial="hello">

    <final id="hello">
      <onentry>
        <log expr="'hello world'" />
      </onentry>
    </final>
  </scxml>
  EOXML


class A is XML::Actions::Work {
  method final:start ( Array $parent-path, :$id ) {
    is $id, 'hello', "final called: id = $id";
    is $parent-path[*-1].name, 'final', 'this node is final';
    is $parent-path[*-2].name, 'scxml', 'parent node is scxml';

  method log:start ( Array $parent-path, :$expr ) {
    is $expr, "'hello world'", "log called: expr = $expr";
    is-deeply @$parent-path.map(*.name), <scxml final onentry log>,
              "<scxml final onentry log> found in parent array";
  }
}

my XML::Actions $a .= new(:$file);
isa-ok $a, XML::Actions, 'type ok';
$a.process(:actions(A.new()));

Result would be like

ok 1 - type ok
ok 2 - final called: id = hello
ok 3 - this node is final
ok 4 - parent node is scxml
ok 5 - log called: expr = 'hello world'
ok 6 - <scxml final onentry log> found in parent array

Documentation

Users who wish to process XML::Elements (from the XML package) must provide an instantiated class which inherits from XML::Actions::Work. In that class, methods named after the elements can be defined. Those methods must have :start or :end attached to have them called when <someElement> or </someElement> is encountered.

class A is XML::Actions::Work {

  method someElement:start ( Array $parent-path, *%attrs --> ActionResult )
  method someElement:end ( Array $parent-path, *%attrs )

  method someOtherElement:start ( Array $parent-path, *%attrs --> ActionResult )
  method someOtherElement:end ( Array $parent-path, *%attrs )
}

There are also text-, comment-, cdata- and pi-nodes. They can be processed as

  method xml:text ( Array $parent-path, Str $text ) {...}
  method xml:comment ( Array $parent-path, Str $comment ) {...}
  method xml:cdata ( Array $parent-path, Str $cdata ) {...}
  method xml:pi ( Array $parent-path, Str $pi-target, Str $pi-content ) {...}

Large files

When you have to process a large file (e.g. an XML file holding POI data of Turkey from osm planet is about 6.2 Gb), one cannot use the XML::Actions module because the DOM tree is build in memory. Since version 0.4.0 the package is extended with module XML::Actions::Stream which eats the file in chunks of 128 Kb and chops it up into parseble parts. There will be no check on proper XML because it will slow down too much. You can use other tools for that. There are a few more methods possible to be defined by the user. The arguments to the methods are the same but the first argument, which is an array, has other type of elements.

User definable methods

The user can define the methods in a class which inherits from XML::Actions::Stream::Work. The methods which the user may define are;

The array is a list of pairs. The key of each pair is the element name and the value is a hash of its attributes. Entry $parent-path[*-1] is the currently called element, so its parent is at *-2. The root element is at 0 and is always available.

Changes

One can find the changes document in ./doc/CHANGES.md

Installing

Use zef to install the package: zef install XML::Actions

AUTHORS

Current maintainer Marcel Timmerman (MARTIMM on github)

License

Artistic-2.0