YAX is a fast pure Perl XML library for easily parsing, querying, constructing and manipulating XML. Simple benchmarks have shown that it is substantially faster than XML::DOM::Parser which uses Expat internally (which is written in C), see "PERFORMANCE" for an explanation and related caveats.
However, the main point of YAX is to remove the verbosity of the DOM API by using more perlish tricks such as operator overloading. For example, element nodes can behave as both array references and hash references. If dereferenced as an array reference, then a list of children is returned; as a hash reference, the attributes hash is returned. So the following show uses cases for this:
my @good_books = grep { $_->{author} =~ /Asimov/ } @$elmt;
You can also hang out of band data onto your elements:
$elmt->{notes} = IO::File->new( './notes/asimov.txt' );
without affecting serialization since attributes who's values are references are ignored during stringification.
YAX nodes, of course, also provide methods for appending, replacing and removing children as well (note the following all operate on the children of $node):
$node->replace( $new_child, $ref_child );
$node->remove( $child );
$node->append( $child );
$node->insert( $new_child, $ref_child );
SYNOPSIS
use YAX::Parser;
# DOM parse
my $xdoc = YAX::Parser->parse( $xstr );
my $xdoc = YAX::Parser->parse_file( '/some/file.xml' );
# stream parse
YAX::Parser->stream( $xstr, $state,
text => &parse_text,
elmt => &parse_element_open,
elcl => &parse_element_close,
... # see YAX::Parser
);
YAX::Parser->stream_file( '/some/file.xml', $state, %handlers );
# access the document root
my $root = $xdoc->root;
# get an element by id
my $elmt = $xdoc->get( 'foo' );
# attribute access
$elmt->{meaning} = 42;
# loop over children
for my $child ( @$elmt ) {
...
}
# query the DOM
my $nlst = $elmt->query(q{..a.b.*.(@foo eq 'bar')[1 .. 2]});
# declarative programmatic DOM construction
use YAX::Builder;
my $node = YAX::Builder->node(
[ table => { border => 1 },
[ tr =>
[ td => { align => 'top' },
[ a => { href => '/foo' }, "Click Me!" ]
]
]
]
);
# serialization
my $xstr = $node->as_string;
my $xstr = "$node"; # '""' is overloaded
Product's homepage
Requirements:
· Perl