home *** CD-ROM | disk | FTP | other *** search
/ Chip: Windows 2000 Professional Resource Kit / W2KPRK.iso / apps / perl / ActivePerl.exe / data.z / Element.pm < prev    next >
Encoding:
Text File  |  1999-10-14  |  3.1 KB  |  123 lines

  1. #
  2. # XML::Element
  3. #
  4. # Base class for XML Elements.  Provides the ability to output the XML document
  5. # once it's been parsed using the XML::Parser module.
  6. #
  7. ###############################################################################
  8.  
  9. $XML::Element::revision = '$Id: Element.pm,v 1.1.1.1 1998/09/23 18:50:25 graham Exp $';
  10. $XML::Element::VERSION  = '0.10';
  11.  
  12. ###############################################################################
  13. # Define the basic element type.
  14. ###############################################################################
  15. package XML::Element;
  16.  
  17. use HTML::Entities;
  18.  
  19. ###############################################################################
  20. # Allow for creation via 'new'.
  21. ###############################################################################
  22. sub new
  23. {
  24.     my ($class, %args) = @_;
  25.     bless \%args, $class;
  26. }
  27.  
  28. ###############################################################################
  29. # Output the whole XML document (from here on down)
  30. ###############################################################################
  31. sub output
  32. {
  33.     my $self = shift;
  34.     my $type= ref $self;
  35.     $type =~ s/.*:://;
  36.  
  37.     print '<' . $type;
  38.     foreach (sort keys %{$self})
  39.     {
  40.         if ($_ !~ /Text|Kids/)
  41.             { print " $_=\"" . $self->{$_} . '"'; }
  42.     }
  43.     my @kids = @{ $self->{Kids} };
  44.     if ($#kids >= 0)
  45.     {
  46.         print '>';
  47.         foreach (@kids)
  48.         {
  49.             # Allow for outputting of char data unless overridden
  50.             if ((ref $_) =~ /::Characters$/o)
  51.             {
  52.                 print encode_entities( $_->{Text} );
  53.             }
  54.             else
  55.             {
  56.                 $_->output();
  57.             }
  58.         }
  59.         print '</' . $type . '>';
  60.     }
  61.     else
  62.     {
  63.         print ' />';
  64.     }
  65. }
  66.  
  67. __END__
  68.  
  69. ###############################################################################
  70. # Embedded POD for the PPD element.
  71. ###############################################################################
  72.  
  73. =head1 NAME
  74.  
  75. XML::Element - Base element class for XML elements
  76.  
  77. =head1 SYNOPSIS
  78.  
  79.  use XML::Element;
  80.  @ISA = qw( XML::Element );
  81.  
  82. =head1 DESCRIPTION
  83.  
  84. Base element class for XML elements.  To be derived from to create your own
  85. elements for use with the XML::Parser module.  Supports output of empty
  86. elements using <.... />.
  87.  
  88. It is recommended that you use a
  89. version of the XML::Parser module which includes support for Styles; by
  90. deriving your own elements from XML::Element and using the 'Objects' style it
  91. becomes B<much> easier to create your own parser.
  92.  
  93. =head1 METHODS
  94.  
  95. =over 4
  96.  
  97. =item output
  98.  
  99. Recursively outputs the structure of the XML document from this element on
  100. down.
  101.  
  102. =back
  103.  
  104. =head1 LIMITATIONS
  105.  
  106. The C<XML::Element> module has no provisions for outputting processor
  107. directives or external entities.  It only outputs child elements and any
  108. character data which the elements may contain.
  109.  
  110. =head1 AUTHORS
  111.  
  112. Graham TerMarsch <grahamt@activestate.com>
  113.  
  114. =head1 HISTORY
  115.  
  116. v0.1 - Initial version
  117.  
  118. =head1 SEE ALSO
  119.  
  120. L<XML::Parser>
  121.  
  122. =cut
  123.