XML-Generator
view release on metacpan or search on metacpan
lib/XML/Generator/DOM.pm view on Meta::CPAN
=cut
sub xmlpi {
my $this = shift;
my $root = $this->{dom};
my $tgt = shift;
return $root->createProcessingInstruction($tgt, join '', @_);
}
=head2 xmlcmnt
Escaping of '--' is done by XML::DOM::Comment, which replaces both
hyphens with '-'. An XML::DOM::Comment object is returned.
=cut
sub xmlcmnt {
my $this = shift;
my $root = $this->{dom};
my $xml = join '', @_;
return $root->createComment($xml);
}
my $config = 'XML::Generator::util::config';
=head2 xmldecl
Returns an XML::DOM::XMLDecl object. Respects 'version', 'encoding'
and 'dtd' settings in the object.
=cut
sub xmldecl {
my $this = shift;
my $root = $this->{dom};
my $version = $this->$config('version') || '1.0';
my $encoding = $this->$config('encoding') || undef;
my $standalone = $this->xmldtd($this->$config('dtd'))
? "no" : "yes";
return $root->createXMLDecl($version, $encoding, $standalone)
}
=head2 xmldecl
Returns an XML::DOM::DocumentType object.
=cut
sub xmldtd {
my($this, $dtd) = @_;
my $root = $this->{dom};
$dtd ||= $this->$config('dtd');
return unless $dtd && ref($dtd) eq "ARRAY";
return $root->createDocumentType(@{ $dtd });
}
=head2 xmlcdata
Returns an XML::DOM::CDATASection object.
=cut
sub xmlcdata {
my $this = shift;
my $data = join '', @_;
my $root = $this->{dom};
return $root->createCDATASection($data);
}
=head2 xml
As described above, xml() can only be used when dom_document was not
set in the object. The automatically created document will have its XML
Declaration set and the arguments to xml() will be appended to it. Then
a new DOM document is automatically generated and the old one is
returned. This is the only way to get a DOM document from this module.
=cut
sub xml {
my $this = shift;
my $root = $this->{dom};
if ($root != $XML::Generator::DOM::util::root) {
croak "xml() method not allowed when dom_document option specified";
}
$this->{dom} = XML::Generator::DOM::util::new_dom_root();
$root->setXMLDecl($this->xmldecl());
$root->appendChild($_) for @_;
return $root;
}
sub AUTOLOAD {
my $this = shift;
(my $tag = $AUTOLOAD) =~ s/.*:://;;
my $root = $this->{'dom'};
my($namespace, $attr, @args) = $this->XML::Generator::util::parse_args(@_);
$namespace = $namespace->[1] ? $namespace->[1] . ':' : '';
my $xml = $root->createDocumentFragment();
my $node = $xml->appendChild($root->createElement("$namespace$tag"));
if ($attr) {
while (my($k, $v) = each %$attr) {
unless ($k =~ /^[^:]+:/) {
$k = "$namespace$k";
}
$node->setAttribute($k, $v);
}
}
for (@args) {
if (UNIVERSAL::isa($_, 'XML::DOM::Node')) {
$node->appendChild($_);
} else {
( run in 2.505 seconds using v1.01-cache-2.11-cpan-cdf2f3d4e48 )