XML-DOM2

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

	Updated Parser to include parent creation of elements
	Moved XPath method to it's own module
0.04    2007-11-19
	Cleaned up many pod problems
0.05    2007-11-19
	Fixed errors caused by 0.04
	Added overload to attributes
	Added simple xml loading test
0.06    2007-11-29
  Added generating xml documents from perl structures
  Fixed many bugs in cdata and createChild methods
  Cleaned up pod and updated tests

lib/XML/DOM2.pm  view on Meta::CPAN


# Adapt a HASH ref into XML
sub _adapt_hash
{
	my ($class, $element, $hash) = @_;

	foreach my $name (keys(%{$hash})) {
		my $data  = $hash->{$name};

		if($name eq '+') {
			$element->cdata($data)
		} elsif($name =~ /^_(.+)$/) {
			$element->setAttribute($1, $data);
		} else {
		  my $isa = UNIVERSAL::isa($data, 'ARRAY');
			my $child = $isa ? $name : $element->createElement( $name );
			$class->_adapt_child( $child, $data, $element );
		}
	}
	return $element;
}

lib/XML/DOM2.pm  view on Meta::CPAN

		$class->_adapt_child( $child, $data, $parent );
	}
	return $parent;
}

# Adapt a SCALAR into XML
sub _adapt_scalar
{
	my ($self, $element, $scalar) = @_;
	if(defined($scalar)) {
		my $result = $element->createElement( '#cdata-entity', text => scalar( $scalar ) );
	}
	return $element;
}

=head2 $object->extension()
	
  $extention = $xml->extention();

  Does not work, legacy option maybe enabled in later versions.

lib/XML/DOM2.pm  view on Meta::CPAN

=cut
sub _element_handle
{
	my ($self, $type, %opts) = @_;
	confess "Element handler with no bleedin type!!" if not $type;
	if($type eq '#document' or $type eq $self->_document_name) {
		$opts{'documentTag'} = $type if $type ne '#document';
		return XML::DOM2::Element::Document->new(%opts);
	} elsif($type eq '#comment') {
		return XML::DOM2::Element::Comment->new( delete($opts{'text'}), %opts);
	} elsif($type eq '#cdata-entity') {
		return XML::DOM2::Element::CDATA->new(delete($opts{'text'}), %opts);
	}
	return XML::DOM2::Element->new( $type, %opts );
}

=head2 $object->_option( $name[, $data] )

  Set or get the required option.

=cut

lib/XML/DOM2/DOM/Document.pm  view on Meta::CPAN

}

=head2 $document->createTextNode( $data )

  Create a textnode element.

=cut 
sub createTextNode
{
	my ($self, $data) = @_;
	return $self->_element_handle( '#cdata-entity', notag => 1 );
}

=head2 $document->createComment( $data )

  Create a comment element

=cut 
sub createComment
{
	my ($self, $data) = @_;

lib/XML/DOM2/DOM/Document.pm  view on Meta::CPAN

}

=head2 $document->createCDATASection( $data )

  create a CDATA element.

=cut 
sub createCDATASection
{
	my ($self, $data) = @_;
	return $self->_element_handle( '#cdata-entity',	notag => 0 );
}

=head1 COPYRIGHT

Martin Owens, doctormo@cpan.org

=head1 SEE ALSO

L<XML::DOM2>

lib/XML/DOM2/DOM/Element.pm  view on Meta::CPAN

	if(not ref($ns)) {
		confess "You must give ns methods the name space object, not just the URI or Prefix (skipped)";
	}
	my $prefix = $ns->ns_prefix;
	$self->{'attributes'}->{$prefix}->{$name} = $self->_get_attribute_object($name, $value, $ns);
	if(not $self->{'attributes'}->{$prefix}->{$name}) {
		warn "setAttributeNS was unable to set the attribute ";
	}
}

=head2 $element->cdata( $text )

  Rerieve and set this elements cdata (non tag cdata form)

=cut
sub cdata
{
	my ($self, $text) = @_;
	if($self->hasChildren()) {
		$self->error(value => "Unable to get cdata for element with children, xml error!");
		return;
	}
	if(defined($text)) {
		if(ref($text) =~ /CDATA/) {
			$self->{'cdata'} = $text;
		} else {
			$self->{'cdata'} = XML::DOM2::Element::CDATA->new($text, notag => 1);
		}
	}
	return $self->{'cdata'};
}

=head2 $element->hasCDATA()

  Return true if this element has cdata.

=cut
sub hasCDATA ($) {
	my $self=shift;
	return exists($self->{'cdata'});
}

=head2 $element->document()

  Return this elements document, returns undef if no document available.

=cut
sub document
{
	my ($self) = @_;

lib/XML/DOM2/DOM/Element.pm  view on Meta::CPAN


$element->createChildElement($name, %opts);

=cut

sub createChildElement
{
	my ($self, $name, %opts) = @_;
	my $element = $self->_element_handle($name, %opts, document => $self->document() );
	if(ref($element) =~ /CDATA/) {
		$self->cdata( $element );
	} else {
		$self->appendChild($element);
	}
	return $element;
}
*createElement=\&createChildElement;

=head1 AUTHOR

Martin Owens, doctormo@postmaster.co.uk

lib/XML/DOM2/Element.pm  view on Meta::CPAN

		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;
}

=head2 $element->_element_handle()

lib/XML/DOM2/Element.pm  view on Meta::CPAN

	}

    $self->{errors}{$command}=$error;
}

=head1 OVERLOADED

=head2 $object->auto_string()

=cut
sub auto_string { return $_[0]->hasCDATA() ? $_[0]->cdata() : '' }

=head2 $object->auto_eq( $string )

=cut
sub auto_eq { return shift->auto_string() eq shift }

=head2 BEGIN()

  POD Catch, imagened method.

lib/XML/DOM2/Element/CDATA.pm  view on Meta::CPAN

use strict;
use warnings;

use overload
	'""' => sub { shift->auto_string( @_ ) },
	'eq' => sub { shift->auto_eq( @_ ) },
	'ne' => sub { not shift->auto_eq( @_ ) };

=head2 $class->new( $text, %arguments )

  Create a new cdata object.

=cut
sub new
{
	my ($proto, $text, %args) = @_;
	$args{'text'} = $text;
	my $self = $proto->SUPER::new('cdata', %args);
	return $self;
}

=head2 $element->xmlify()

  Returns the text as a serialised xml string (serialisation)

=cut
sub xmlify
{

lib/XML/DOM2/Parser.pm  view on Meta::CPAN

    # ELEMENT
	# LocalName - The name of the element minus any namespace prefix it may have come with in the document.
	# NamespaceURI - The URI of the namespace associated with this element, or the empty string for none.
	# Name - The name of the element as it was seen in the document (i.e.  including any prefix associated with it)
	# Prefix - The prefix used to qualify this element’s namespace, or the empty string if none.
	$self->{'parent'} = pop @{$self->{'parents'}};
}

=head2 $parser->characters()

  Handle part of a cdata by concatination

=cut
sub characters
{
	my ($self, $text) = @_;

	$text = $text->() if ref($text) eq 'CODE';
	# We wish to keep track of text characters, and
	# and deal with text once other elements are found
	$self->{'text'} = '' if not defined($self->{'-text'});
	$self->{'text'} .= $text->{'Data'};
}

=head2 $parser->text()

  Handle combined text strings as cdata

=cut
sub text
{
	my ($self) = @_;
	if($self->{'text'}) {
		my $text = $self->{'text'};
		if($text =~ /\S/) {
			$self->{'parent'}->cdata($text);
		}
		delete($self->{'text'});
	}
}

=head2 $parser->comment()

 WARNING: Comments are currently removed!

=cut
sub comment
{
	my ($self, $comment) = @_;
	$self->text;
#	warn "Comment '".$comment->{'Data'}."'\n";
	# Data
}

=head2 $parser->start_cdata()

  Never used by parser.

=cut
sub start_cdata
{
	print STDERR "START CDATA\n";
}

=head2 $parser->end_cdata()

  Never used by parser.

=cut
sub end_cdata
{
	print STDERR "END CDATA\n";
}

=head2 $parser->processing_instruction()

  Never used by parser.

=cut
sub processing_instruction

t/01-xml-load.t  view on Meta::CPAN

my $child = $sibling->getFirstChild();

ok( $child, 'Get first child' );
ok( $child->localName() eq 'child21', 'Child tag name' );

my $attr = $child->getAttribute( 'attributeB' );
ok( $attr, 'Load Attribute from element' );
ok( $attr->value() eq 'valueB', 'Attribute value' );
ok( $attr eq 'valueB', 'Attribute Overloaded' );

ok( $child->cdata()->text() eq 'value2', 'Element cdata contents' );

exit 0;



( run in 0.637 second using v1.01-cache-2.11-cpan-454fe037f31 )