Zahatski Aliaksandr > XML-Handler-ExtOn-0.06 > XML::Handler::ExtOn



Annotate this POD


New  1
Open  0
View/Report Bugs
Module Version: 0.06   Source  


XML::Handler::ExtOn - The handler for expansion of Perl SAX by objects.


    use XML::Handler::ExtOn;

For write XML:

    use XML::Handler::ExtOn;
    my $buf;
    my $wrt = XML::SAX::Writer->new( Output => \$buf );
    my $ex_parser = new XML::Handler::ExtOn:: Handler => $wrt;
    my $root = $ex_parser->mk_element("Root");
        "myns" => '',
        "myns_test", ''
    $ex_parser->start_element( $root );
    my $el = $root->mk_element('vars');
    %{ $el->attrs_by_prefix("myns") }      = ( v1 => 1, v2 => 3 );
    %{ $el->attrs_by_prefix("myns_test") } = 
    ( var1 => "test ns", var2 => "2333" );
    print $buf;


    <?xml version="1.0"?>
    <Root xmlns:myns="" 
    <vars myns_test:var2="2333" 
        myns_test:var1="test ns" 
        myns:v1="1" myns:v2="3"/>

For handle events

    use base 'XML::Handler::ExtOn';

Begin method for handle SAX event start_element:

    sub on_start_element {
        my ( $self, $elem ) = @_;


Check localname for element and add tag image:

        if ( $elem->local_name eq 'gallery' ) {
                        $self->mk_characters( "Image number: $_" )
                  ) for 1..2 ;

XML Before:

    <?xml version="1.0"?>


    <?xml version="1.0"?>
        <image>Image number: 1</image>
        <image>Image number: 2</image>

Register namespace and set variables

        #set attributes for name space
        my $demo_attrs = $elem->attrs_by_prefix('demons');
        %{$demo_attrs} = ( variable1=>1, 'variable2'=>2);
        #set attributes for namespace URI
        my $ns2_attrs = $elem->attrs_by_ns_uri('');
        %{$ns2_attrs} = ( var=> 'ns1', 'raw'=>2);


        <sub xmlns:demons="" 
            demons:variable2="2" ns2:var="ns1" 
            demons:variable1="1" ns2:raw="2"/>

Delete content of element

    if ( $elem->local_name eq 'demo_delete') {

XML before:

    <?xml version="1.0"?>


    <?xml version="1.0"?>

Add XML:

        $elem->add_content ( 
Can add element after current

        return [ $elem, $self->mk_element("after") ];


XML::Handler::ExtOn - SAX Handler designed for funny work with XML. It provides an easy-to-use interface for XML applications by adding objects.

XML::Handler::ExtOn override some SAX events. Each time an SAX event starts, a method by that name prefixed with `on_' is called with the "blessed" Element object to be processed.

XML::Handler::ExtOn implement the following methods:

XML::Handler::ExtOn put all cdata characters into a single event on_cdata.

It compliant XML namespaces (, by support default namespace and namespace scoping.

XML::Handler::ExtOn provide methods for create XML, such as mk_element, mk_cdata ...


create_pipe "flt_n1",$some_handler, $out_handler ^

use last arg as handler for out.

return parser ref.

    my $h1     = new MyHandler1::;
    my $filter = create_pipe( 'MyHandler1', $h1 );


on_start_document $document

Method handle start_document event. Usually override for initialaize default variables.

    sub on_start_document {
        my $self = shift;
        $self->{_LINKS_ARRAY} = [];

on_start_prefix_mapping prefix1=>ns_uri1[, prefix2=>ns_uri2]

Called on start_prefix_mapping event.

    sub on_start_prefix_mapping {
        my $self = shift;
        my %map  = @_;

on_start_element $elem

Method handle on_start_element event whith XML::Handler::ExtOn::Element object.

Method must return $elem or ref to array of objects.

For example:

    sub on_start_element {
        my $self = shift;
        my $elem = shift;
        $elem->add_content( $self->mk_cdata("test"));
        return $elem
    return [ $elem, ,$self->mk_element("after_start_elem") ]
    return [ $self->mk_element("before_start_elem"), $elem ]

on_end_element $elem

Method handle on_end_element event whith XML::Handler::ExtOn::Element object. It call before end if element.

Method must return $elem or ref to array of objects.

For example:

    sub on_end_element {
        my $self = shift;
        my $elem = shift;
        if ( $elem->is_delete_element ) {
            warn $elem->local_name . " deleted";
            return [ $elem, $self->mk_element("after_deleted_elem") ]
        return $elem
    return [ $elem, ,$self->mk_element("after_close_tag_of_elem") ]
    return [ $self->mk_element("before_close_tag_of_elem"), $elem ]

on_characters( $self->current_element, $data->{Data} )

Must return string for write to stream.

    sub on_characters {
        my ( $self, $elem, $str ) = @_;
        #lowercase all characters
        return lc $str;

on_cdata ( $current_element, $data )

Must return string for write to stream

    sub on_cdata {
        my ( $self, $elem, $str ) = @_;
        return lc $str;

mk_element <tag name>

Return object of element item for include to stream.

mk_from_xml <xml string>

Return command for include to stream.

mk_cdata $string | \$string

return command for insert cdata to stream

mk_characters $string | \$string

return command for insert characters to stream


Return link to current processing element.

add_namespace <Prefix> => <Namespace_URI>, [ <Prefix1> => <Namespace_URI1>, ... ]

Add Namespace mapping. return $self

If Prefix eq '', this namespace will then apply to all elements that have no prefix.

        "myns" => '',
        "myns_test", '',


XML::Handler::ExtOn::Element, XML::SAX::Base


Zahatski Aliaksandr, <>


Copyright (C) 2007-2008 by Zahatski Aliaksandr

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.

syntax highlighting: