XML-Parser-Wrapper
view release on metacpan or search on metacpan
lib/XML/Parser/Wrapper.pm view on Meta::CPAN
}
}
*content = \&text;
*contents = \&text;
*getText = \&text;
*getContent = \&text;
*getContents = \&text;
=pod
=head2 C<html()>
Like text(), except HTML-escape the text (escape &, <, >, and ")
before returning it.
Aliases: content_html(), getContentHtml()
=cut
sub html {
my $self = shift;
return $self->escape_html($self->text);
}
*content_html = \&html;
*getContentHtml = \&html;
=pod
=head2 C<xml()>
Like text(), except XML-escape the text (escape &, <, >, and ")
before returning it.
Aliases: content_xml(), getContentXml()
=cut
sub xml {
my $self = shift;
return $self->escape_xml_attr($self->text);
}
*content_xml = \&html;
*getContentXml = \&html;
=pod
=head2 C<to_xml(\%options)>
Converts the node back to XML. The ordering of attributes may
not be the same as in the original XML, and CDATA sections may
become plain text elements, or vice versa. This assumes the data
is encoded in utf-8.
Valid options
=head3 pretty
If pretty is a true value, then whitespace is added to the output
to make it more human-readable.
=head3 cdata
If cdata is defined, any text nodes with length greater than
cdata are output as a CDATA section, unless it contains "]]>", in
which case the text is XML escaped.
Aliases: toXml()
=head3 decl
If a true value, output an XML declaration before outputing the
converted document, i.e.,
=for pod2rst next-code-block: xml
<?xml version="1.0" encoding="UTF-8"?>
=cut
sub to_xml {
my ($self, $options) = @_;
unless ($options and ref($options) and UNIVERSAL::isa($options, 'HASH')) {
$options = { };
}
if ($options->{decl}) {
my $xml = qq{<?xml version="1.0" encoding="UTF-8"?>};
if ($options->{pretty}) {
$xml .= "\n";
}
$xml .= $self->_to_xml(0, $options, 0);
return $xml;
}
return $self->_to_xml(0, $options, 0);
}
sub _to_xml {
my ($self, $level, $options, $index) = @_;
unless ($options and ref($options) and UNIVERSAL::isa($options, 'HASH')) {
$options = { };
}
if ($self->is_text) {
my $text = $self->text;
if (defined $options->{cdata}) {
if (length($text) >= $options->{cdata}) {
unless (index($text, ']]>') > -1) {
return '<![CDATA[' . $text . ']]>';
}
}
}
return $self->escape_xml_body($text);
}
my $pretty = $options->{pretty};
my $attributes = $self->_get_attrs;
my $name = $self->name;
my $kids = $self->kids;
my $indent = $pretty ? (' ' x $level) : '';
my $eol = $pretty ? "\n" : '';
my $xml = '';
if ($pretty and $level >= 1) {
$xml .= $eol if $index == 0;
}
$xml .= qq{$indent<$name};
if ($attributes and %$attributes) {
my @pairs;
foreach my $key (sort keys %$attributes) {
my $val = $attributes->{$key} . '';
push @pairs, $key . '=' . '"' . $self->escape_xml_attr($val) . '"';
}
$xml .= ' ' . join(' ', @pairs);
}
if ($kids and @$kids) {
my $cnt = 0;
$xml .= '>' . join('', map { $_->_to_xml($level + 1, $options, $cnt++) } @$kids);
$xml .= $indent if scalar(@$kids) > 1;
$xml .= "</$name>$eol";
}
else {
$xml .= "/>$eol";
}
}
*toXml = \&to_xml;
sub to_jsonml {
my ($self) = @_;
return $self->_to_jsonml;
}
sub _to_jsonml {
my ($self) = @_;
if ($self->is_text) {
return $self->_quote_json_str($self->text);
}
( run in 0.815 second using v1.01-cache-2.11-cpan-39bf76dae61 )