XML-Sablotron
view release on metacpan or search on metacpan
DOM/SAXBuilder.pm view on Meta::CPAN
$self->{Parent} = undef;
$self->{Doc} = undef;
return $doc;
}
sub start_element {
my ($self, $element) = @_;
my $e = $self->{Doc}->createElement($element->{Name});
$self->{Parent}->appendChild($e);
$self->{Parent} = $e;
# attributes
my @keys = keys %{$element->{Attributes}};
if (ref($element->{Attributes}->{$keys[0]})) {
#SAX2 style
foreach (keys %{$element->{Attributes}}) {
$e->setAttribute($element->{Attributes}->{$_}->{Name},
$element->{Attributes}->{$_}->{Value});
}
} else {
#SAX1 style
$e->setAttributes($element->{Attributes});
}
}
sub end_element {
my ($self, $element) = @_;
unless ($self->{Parent} == $self->{Doc}) {
$self->{Parent} = $self->{Parent}->getParentNode();
}
}
sub characters {
my ($self, $data) = @_;
if ($self->{CDATA}) {
$self->{Parent}->appendChild(
$self->{Doc}->createCDATASection($data->{Data})
);
} else {
$self->{Parent}->appendChild(
$self->{Doc}->createTextNode($data->{Data})
);
}
}
sub ignorable_whitespace{
my ($self, $data) = @_;
}
sub processing_instruction {
my ($self, $pi) = @_;
$self->{Parent}->appendChild(
$self->{Doc}->createProcessingInstruction($pi->{Target},$pi->{Data})
);
}
sub start_cdata {
my ($self) = @_;
$self->{CDATA} = 1;
}
sub end_cdata {
my ($self) = @_;
delete $self->{CDATA};
}
sub comment {
my ($self, $comment) = @_;
$self->{Parent}->appendChild(
$self->{Doc}->createComment($comment->{Data})
);
}
1;
__END__
# Below is a documentation.
=head1 NAME
XML::Sablotron::SAXBuilder - builds a Sablotron DOM document from SAX events
=head1 SYNOPSIS
use XML::Sablotron;
use XML::Sablotron::DOM;
use XML::Sablotron::SAXBuilder;
use XML::Directory;
$dir = new XML::Directory($path);
$builder = new XML::Sablotron::SAXBuilder;
$doc = $dir->parse_SAX($builder);
=head1 DESCRIPTION
This is a SAX handler generating a Sablotron DOM tree from SAX events.
Input should be accepted from any SAX1 or SAX2 event generator. This
handler implements all methods required for basic Perl SAX 2.0 handler
and some of the advanced methods (that make sense for Sablotron DOM tree).
In particular, the following methods are available:
=over
=item start_document
=item end_document
=item start_element
=item end_element
=item characters
=item ignorable_whitespace
=item processing_instruction
=item start_cdata
=item end_cdata
=item comment
=back
Namespaces are not supported by XML::Sablotron::DOM yet, therefore SAX2
events are accepted but NS information is ignored.
=head1 LICENSING
Copyright (c) 2001 Ginger Alliance. All rights reserved. This program is free
software; you can redistribute it and/or modify it under the same terms as
Perl itself.
=head1 AUTHOR
Petr Cimprich, petr@gingerall.cz
=head1 SEE ALSO
XML::Sablotron, XML::Sablotron::DOM, perl(1).
=cut
( run in 2.291 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )