XML::Chain - chained way of manipulating and inspecting XML documents
use XML::Chain qw(xc); # basics my $div = xc('div', class => 'pretty') ->c('h1')->t('hello') ->up ->c('p', class => 'intro')->t('world') ->root ->a( xc('p')->t('of chained XML.') ); say $div->as_string; # <div class="pretty"><h1>hello</h1><p class="intro">world</p><p>of chained XML.</p></div> my $sitemap = xc('urlset', xmlns => 'http://www.sitemaps.org/schemas/sitemap/0.9') ->t("\n") ->c('url') ->a('loc', '-' => 'https://metacpan.org/pod/XML::Chain::Selector') ->a('lastmod', '-' => DateTime->from_epoch(epoch => 1507451828)->strftime('%Y-%m-%d')) ->a('changefreq', '-' => 'monthly') ->a('priority', '-' => '0.6') ->up->t("\n") ->c('url') ->a('loc', '-' => 'https://metacpan.org/pod/XML::Chain::Element') ->a('lastmod', '-' => DateTime->from_epoch(epoch => 1507279028)->strftime('%Y-%m-%d')) ->a('changefreq', '-' => 'monthly') ->a('priority', '-' => '0.5') ->up->t("\n"); say $sitemap->as_string; # <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> # <url><loc>https://metacpan.org/pod/XML::Chain::Selector</loc><lastmod>2017-10-08</lastmod><changefreq>monthly</changefreq><priority>0.6</priority></url> # <url><loc>https://metacpan.org/pod/XML::Chain::Element</loc><lastmod>2017-10-06</lastmod><changefreq>monthly</changefreq><priority>0.5</priority></url> # </urlset>
This module provides fast and easy way to create and manipulate XML elements via set of chained method calls.
Exported factory method creating new XML::Chain::Selector object with a document element as provided in parameters. For example:
my $icon = xc('i', class => 'icon-download icon-white'); # <i class="icon-download icon-white"/>
See "c, append_and_current" in XML::Chain::Selector for the element parameter description and "CHAINED METHODS" in XML::Chain::Selector for methods of returned object.
Element with $name will be create as document element and @attrs will be added to it in the same order.
$name
@attrs
In case of hash reference passed as argument, key + values will be set as attributes, in alphabetical sorted key name order.
Attribute name "-" is a special case and the value will used for text content inside the element.
In case of XML::LibXML, it will be set as document element.
Any other reference will be passed to "slurp($what)" in IO::Any which will be then parsed by "load_xml" in XML::LibXML and result set as document element.
say xc([$tmp_dir, 't01.xml'])->as_string say xc(\'<body><h1>and</h1><h1>head</h1></body>') ->find('//h1')->count
Element with $scalar will be create as document element.
$scalar
say xc('body');
See XML::Chain::Selector and XML::Chain::Element.
xc('body')->t('save me')->set_io_any([$tmp_dir, 't01.xml'])->store; # $tmp_dir/t01.xml file now consists of: <body>save me</body> xc([$tmp_dir, 't01.xml'])->empty->c('div')->t('updated')->store; # $tmp_dir/t01.xml file now consists of: <body><div>updated</div></body>
Store $what of IO::Any for future use with -store() >
$what
-
Calls IO::Any-spew($io_any, $self->as_string, {atomic => 1}) > to save XML back it it's original file of the the target set via set_io_any.
IO::Any-
set_io_any
- partial/special tidy (on elements inside xml) - per ->data() storage - ->each(sub {...}) / ->map(sub {}) / ->grep(sub {}) - setting and handling namespaces and elements with ns prefixes - ~ton of selectors and manipulators to be added
Initially inspired by Strophe.Builder, then also by jQuery.
The following people have contributed to the XML::Chain by committing their code, sending patches, reporting bugs, asking questions, suggesting useful advice, nitpicking, chatting on IRC or commenting on my blog (in no particular order):
Vienna.pm (for listening to my talk and providing valuable feedback) Mohammad S Anwar you?
Also thanks to my current day-job-employer http://geizhals.at/.
Please report any bugs or feature requests via https://github.com/meon/XML-Chain/issues.
Jozef Kutej
Copyright 2017 Jozef Kutej, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install XML::Chain, copy and paste the appropriate command in to your terminal.
cpanm
cpanm XML::Chain
CPAN shell
perl -MCPAN -e shell install XML::Chain
For more information on module installation, please visit the detailed CPAN module installation guide.