XML::Genx::Simple - A slightly simpler wrapper class for genx


XML-Genx documentation Contained in the XML-Genx distribution.

Index


Code Index:

NAME

Top

XML::Genx::Simple - A slightly simpler wrapper class for genx

SYNOPSIS

Top

  use XML::Genx::Simple;
  my $w = XML::Genx::Simple->new;
  eval {
    # <root><foo id="1">bar</foo></root>
    $w->StartDocFile( *STDOUT );
    $w->StartElementLiteral( 'root' );
    $w->Element( foo => 'bar', id => 1 );
    $w->EndElement;
    $w->EndDocument;
  };
  die "Writing XML failed: $@" if $@;

DESCRIPTION

Top

This class provides some helper methods to make using XML::Genx simpler in the common case.

METHODS

Top

StartDocString ( )

Starts a new document, and collects the result into a string.

This method is offered as an extension to the genx API since it is significantly quicker. Many thanks to A. Pagaltzis for suggesting it.

GetDocString ( )

Returns the string from the current writer object.

NB: This is only guaranteed to be well-formed XML after you have called EndDocument().

NB: This will only produce sensible output if you've called StartDocString() previously.

Element ( NAME, TEXT, [ATTRS] )

Outputs <NAME>TEXT</NAME> in one go. NAME can be either a text string or an XML::Genx::Element object.

If NAME is a text string, an XML::Genx::Element object will be created, used and cached. So there's no real advantage to passing one in.

Optionally, ATTRS can be passed in as a list of key/value pairs. Again, each key used as an attribute name will be cached.

This method provides no namespace support.

SEE ALSO

Top

XML::Genx.

AUTHOR

Top

Dominic Mitchell, <cpan (at) happygiraffe.net>

The genx library was created by Tim Bray http://www.tbray.org/.

COPYRIGHT AND LICENSE

Top

VERSION

Top

@(#) $Id: Simple.pm 1270 2006-10-08 17:29:33Z dom $


XML-Genx documentation Contained in the XML-Genx distribution.

# @(#) $Id: Simple.pm 1270 2006-10-08 17:29:33Z dom $

package XML::Genx::Simple;

use strict;
use warnings;

use base 'XML::Genx';

our $VERSION = '0.22';

sub Element {
    my $self = shift;
    my ( $name, $text, %attrs ) = @_;

    # Sadly, we can't cache a copy when it's a reference (presumably
    # an XML::Genx::Element), because we have no easy way to get at
    # the actual name through the pointer.
    my $el = ref $name ? $name : $self->_DeclaredElement( $name );

    $el->StartElement;
    while ( my ( $name, $val ) = each %attrs ) {
        my $at = $self->_DeclaredAttribute( $name );
        $at->AddAttribute( $val );
    }
    $self->AddText( $text );
    $self->EndElement;
}

sub DESTROY {
    my $self = shift;
    # Clean up any loose pointers we have...
    $self->_UndeclareElements;
    $self->_UndeclareAttributes;

    # And pass control back to our parents.
    $self->SUPER::DESTROY;
}

#---------------------------------------------------------------------
# Private down here.
#---------------------------------------------------------------------

{
    # Because we don't have anywhere inside $self to store the extra
    # information we want, we need to use some private storage.
    my %el;
    sub _DeclaredElement {
        my $self = shift;
        my ( $name ) = @_;
        return $el{ $self }{ $name } ||= $self->DeclareElement( $name );
    }
    sub _UndeclareElements {
        my $self = shift;
        delete $el{ $self };
    }
}

{
    # Ditto about lack of storage in self.
    my %att;
    sub _DeclaredAttribute {
        my $self = shift;
        my ( $name ) = @_;
        return $att{ $self }{ $name } ||= $self->DeclareAttribute( $name );
    }
    sub _UndeclareAttributes {
        my $self = shift;
        delete $att{ $self };
    }
}

1;
__END__