XML-Parser-Wrapper
view release on metacpan or search on metacpan
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
pretty
If pretty is a true value, then whitespace is added to the output to
make it more human-readable.
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()
decl
If a true value, output an XML declaration before outputing the
converted document, i.e.,
<?xml version="1.0" encoding="UTF-8"?>
lib/XML/Parser/Wrapper.pm view on Meta::CPAN
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
lib/XML/Parser/Wrapper.pm view on Meta::CPAN
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};
t/01element.t view on Meta::CPAN
use Test::More tests => 5;
use XML::Parser::Wrapper;
my $xml = q{<?xml version="1.0" encoding="utf-8"?><!DOCTYPE greeting SYSTEM "hello.dtd"><store><field name="" class="Hash" id="a"><field name="array1" class="Array" id="b"><element name="" class="String" id="c">data with ]]></element><element ...
my $root = XML::Parser::Wrapper->new($xml);
ok(($root->name eq 'store'), 'top name check');
ok(($root->kid('field')->attribute('id') eq $root->kids('field')->[0]->attribute('id')), 'attr check');
ok(($root->kid('field')->kid('field')->kid('element')->text eq 'data with ]]>'), 'cdata');
my $expected_xml_decl = {
'version' => '1.0',
'standalone' => undef,
'encoding' => 'utf-8'
};
my $expected_doctype = {
'pubid' => undef,
'sysid' => 'hello.dtd',
'name' => 'greeting',
}
plan tests => 9;
use XML::Parser::Wrapper;
my $have_io_scalar;
eval { require IO::Scalar; };
$have_io_scalar = 1 unless $@;
my $xml = qq{<response><stuff>stuff val</stuff><more><stuff><foo><stuff>Hidden Stuff</stuff></foo></stuff></more><deep><deep_var1>deep val 1</deep_var1><deep_var2>deep val 2</deep_var2></deep><stuff>more stuff</stuff><some_cdata><![CDATA[blah]]></som...
my @text;
my $handler = sub {
my ($root) = @_;
my $text = $root->text;
push @text, $text;
};
}, { file => $file });
ok(scalar(@text) == 2 and $text[0] eq 'stuff val' and $text[1] eq 'more stuff');
}
else {
skip("Skip cuz couldn't find test file", 1);
}
$xml = qq{<response xmlns:a="http://owensnet.com/schema"><stuff n="foo" a:o="bar>"><level2><level3>code</level3><level3>"fu"</level3></level2></stuff><more><stuff><foo><stuff>Hidden Stuff</stuff></foo></stuff></more><deep><deep_var1>deep val 1</...
my $count = 0;
$handler = sub {
my ($root) = @_;
$count++;
if ($count == 1) {
my $attr = $root->attr('n');
ok($attr eq 'foo');
};
$root = XML::Parser::Wrapper->new_sax_parser({ class => 'XML::LibXML::SAX',
handler => $handler,
start_tag => 'stuff',
# start_depth => 2,
}, $xml);
if ($have_io_scalar) {
$xml = qq{<response><stuff>stuff val</stuff><more><stuff><foo><stuff>Hidden Stuff</stuff></foo></stuff></more><deep><deep_var1>deep val 1</deep_var1><deep_var2>deep val 2</deep_var2></deep><stuff>more stuff</stuff><some_cdata><![CDATA[blah]]></som...
@text = ();
$handler = sub {
my ($root) = @_;
my $text = $root->text;
push @text, $text;
};
my $io = IO::Scalar->new(\$xml);
$root = XML::Parser::Wrapper->new_sax_parser({ class => 'XML::LibXML::SAX',
handler => $handler,
start_tag => 'stuff',
# start_depth => 2,
}, $io);
ok(scalar(@text) == 2 and $text[0] eq 'stuff val' and $text[1] eq 'more stuff');
$xml = qq{<response><stuff>stuff val</stuff><more><stuff><foo><stuff>Hidden Stuff</stuff></foo></stuff></more><deep><deep_var1>deep val 1</deep_var1><deep_var2>deep val 2</deep_var2></deep><stuff>more stuff</stuff><some_cdata><![CDATA[blah]]></...
@text = ();
$handler = sub {
my ($root) = @_;
my $text = $root->text;
push @text, $text;
};
$io = IO::Scalar->new(\$xml);
t/03writer.t view on Meta::CPAN
$listing->set_attr(type => 'old_listing');
my $new_doc = XML::Parser::Wrapper->new_doc('listing', { new => 'listing' });
my $id = $new_doc->add_kid('id', undef, 'new_kid');
$doc->add_kid($new_doc);
my $another_listing = $doc->add_kid('listing');
$another_listing->add_kid('id', undef, 'to be deleted');
my $xml = $doc->to_xml({ cdata => 5 });
ok($xml eq '<feed><listing><id>blah</id></listing><listing foo="bar" type="old_listing"><id>bleh</id></listing><listing new="listing"><id><![CDATA[new_kid]]></id></listing><listing><id><![CDATA[to be deleted]]></id></listing></feed>');
$another_listing->remove_kids;
$xml = $doc->to_xml;
ok($xml eq '<feed><listing><id>blah</id></listing><listing foo="bar" type="old_listing"><id>bleh</id></listing><listing new="listing"><id>new_kid</id></listing><listing/></feed>');
$id->set_text('used to be new');
$xml = $doc->to_xml;
t/data/sax_test.xml view on Meta::CPAN
<response><stuff>stuff val</stuff><more><stuff><foo><stuff>Hidden Stuff</stuff></foo></stuff></more><deep><deep_var1>deep val 1</deep_var1><deep_var2>deep val 2</deep_var2></deep><stuff>more stuff</stuff><some_cdata><![CDATA[blah]]></some_cdata><tric...
( run in 0.381 second using v1.01-cache-2.11-cpan-454fe037f31 )