NAME
XML::Chain - chained way of manipulating and inspecting XML documents
SYNOPSIS
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>
DESCRIPTION
This module provides fast and easy way to create and manipulate XML elements via set of chained method calls.
EXPORTS
xc
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.
xc($name, @attrs) scalar with 1+ arguments
Element with $name
will be create as document element and @attrs
will be added to it in the same order.
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.
xc($xml_libxml_ref)
In case of XML::LibXML, it will be set as document element.
xc($what_ref)
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
xc($scalar)
Element with $scalar
will be create as document element.
say xc('body');
CHAINED METHODS, METHODS and ELEMENT METHODS
See XML::Chain::Selector and XML::Chain::Element.
CHAINED DOCUMENT METHODS
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>
set_io_any
Store $what , $options
of IO::Any for future use with ->store()
store
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
.
TODO
- 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
CONTRIBUTORS & CREDITS
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/.
BUGS
Please report any bugs or feature requests via https://github.com/meon/XML-Chain/issues.
AUTHOR
Jozef Kutej
COPYRIGHT & LICENSE
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.