Data-Validate-XSD

 view release on metacpan or  search on metacpan

lib/Data/Validate/XSD/ParseXML.pm  view on Meta::CPAN

	return $self;
}

=head1 SAX PARSING

=head2 $parser->start_element( $node )

  Start a new xml element

=cut
sub start_element
{
	my ($self, $node) = @_;

	my $name = $node->{'LocalName'};
	my $atrs = $node->{'Attributes'};
	my $ns   = $node->{'Prefix'};
	my $c    = $self->{'current'};
	my $new  = {};

	if(not $c->{$name}) {
		$c->{$name} = $new;
	} else {
		if(ref($c->{$name}) eq 'ARRAY') {
			push @{$c->{$name}}, $new;
		} else {
			$c->{$name} = [ $c->{$name}, $new ];
		}
	}
	push @{$self->{'parents'}}, $c;
	$self->{'count'}++;
	$self->{'name'} = $name;
	$self->{'parent'} = $c;
	$self->{'current'} = $new;

	foreach my $a (keys(%{$atrs})) {
		my $attribute = $atrs->{$a};
		if($attribute->{'Name'} ne 'xmlns') {
			$self->{'current'}->{'_'.$attribute->{'LocalName'}} = $attribute->{'Value'};
		}
	}

}

=head2 $parser->end_element( $element )

  Ends an xml element

=cut
sub end_element
{
	my ($self, $element) = @_;
	$self->{'count'}++;
	$self->{'current'} = $self->{'parent'};
	pop @{$self->{'parents'}};
	$self->{'parent'} = $self->{'parents'}->[$#{$self->{'parents'}}];
}

=head2 $parser->characters()

  Handle part of a cdata by concatination

=cut
sub characters
{
	my ($self, $text) = @_;
	my $t = $text->{'Data'};
	if($t =~ /\S/) {
		my $p = $self->{'parent'};
		my $c = $p->{$self->{'name'}};
		if(ref($c) eq 'HASH') {
			if(%{$c}) {
				if($c->{'+data'}) {
					$c->{'+data'} .= $t;
				} else {
					$c->{'+data'} = $t;
				}
			} else {
				$p->{$self->{'name'}} = $t;
			}
		} elsif(ref($c) eq 'ARRAY') {
			pop @{$c} if ref($c->[$#{$c}]) eq 'HASH' and not %{$c->[$#{$c}]};
			push @{$c}, $t;
		} else {
			$p->{$self->{'name'}} .= $t;
		}
	}
}


=head1 COPYRIGHT

 Copyright, Martin Owens 2007-2008, Affero General Public License (AGPL)

 http://www.fsf.org/licensing/licenses/agpl-3.0.html

=head1 SEE ALSO

L<Data::Validate::XSD>,L<XML::SAX>

=cut
1;



( run in 1.143 second using v1.01-cache-2.11-cpan-d7f47b0818f )