| XML-DOM2 documentation | Contained in the XML-DOM2 distribution. |
XML::DOM2::Element - XML Element level control
Element base class represents an element at the XML level. More specific element classes control the xml functionality which is abstracted from the xml.
Create a new element object.
Returns the element and all it's sub elements as a serialised xml string (serialisation)
Inherited method, returns element which is the specific kind of child object required for this element.
Inherited method, returns attribute as new object or undef.
$attribute = $element->_attribute_handle( $attribute_name, $ns );
Used by XML::DOM2::DOM for auto attribute object handlers.
Inherited method, returns true if attribute has an object.
Used by XML::DOM2::DOM for auto attribute object handlers.
Inherited method, returns true if the element can contain sub elements
Inherited method, returns true if the element can have attributes.
XML ELement serialisation, Open Tag.
XML ELement serialisation, Self contained tag.
XML ELement serialisation, Close Tag.
XML ELement serialisation, Attributes.
Raise an error.
POD Catch, imagened method.
Martin Owens <doctormo@cpan.org> (Fork) Ronan Oger <ronan@roasp.com>
perl(1),L<XML::DOM2>,L<XML::DOM2::Parser>
| XML-DOM2 documentation | Contained in the XML-DOM2 distribution. |
package XML::DOM2::Element; use strict; use warnings;
use base "XML::DOM2::DOM::Element"; use Carp; use XML::DOM2::Attribute; use XML::DOM2::Element::CDATA;
sub new { my ($proto, $name, %opts) = @_; my $class = ref($proto) || $proto; my $self = bless \%opts, $class; $self->{'name'} = $name; return $self; }
sub xmlify { my ($self, %p) = @_; my ($ns, $indent, $level, $sep) = @p{qw/namespace indent level seperator/}; $indent = ' ' if not $indent; $level = 0 if not $level; my $xml = $sep; $xml .= $indent x $level; if($self->hasChildren or $self->hasCDATA) { $xml .= $self->_serialise_open_tag($ns); if($self->hasChildren()) { foreach my $child ($self->getChildren) { $xml .= $child->xmlify( indent => $indent, level => $level+1, seperator => $sep, ); } $xml .= $sep.($indent x $level); } else { $xml .= $self->cdata->text(); } $xml .= $self->_serialise_close_tag(); } else { $xml .= $self->_serialise_tag(); } return $xml; }
sub _element_handle { my ($self, $name, %opts) = @_; if(defined($self->getParent)) { $self->getParent->_element_handle($name, %opts); } elsif($self->document) { $self->document->createElement($name, %opts); } else { croak "Unable to create element, no document or parent node to create against"; } }
sub _attribute_handle { my ($self, $name, %opts) = @_; return XML::DOM2::Attribute->new( name => $name, owner => $self, %opts ); }
sub _has_attribute { 1 }
sub _can_contain_elements { 1 }
sub _can_contain_attributes { 1 }
sub _serialise_open_tag { my ($self) = @_; my $name = $self->name(); my $at = $self->hasAttributes() ? ' '.$self->_serialise_attributes() : ''; return '' if not defined $name; return "<$name$at>"; }
sub _serialise_tag { my ($self) = @_; my $name = $self->name(); my $at= $self->hasAttributes ? ' '.$self->_serialise_attributes : ''; return "<$name$at \/>"; }
sub _serialise_close_tag { my ($self) = @_; my $name = $self->name(); return "</$name>"; }
sub _serialise_attributes { my ($self) = @_; return $self->getAttributes(3); }
sub error ($$$) { my ($self,$command,$error)=@_; confess "Error requires both command and error" if not $command or not $error; if($self->document) { if ($self->document->{-raiseerror}) { die "$command: $error\n"; } elsif ($self->document->{-printerror}) { print STDERR "$command: $error\n"; } } $self->{errors}{$command}=$error; }
sub auto_string { return $_[0]->hasCDATA() ? $_[0]->cdata() : '' }
sub auto_eq { return shift->auto_string() eq shift }
1;