XML::Tiny - simple lightweight parser for a subset of XML
use XML::Tiny qw(parsefile); open($xmlfile, 'something.xml); my $document = parsefile($xmlfile);
The parsefile function is optionally exported. By default nothing is exported. There is no objecty interface.
parsefile
This takes exactly one parameter. That may be:
in which case the file is read and parsed;
in which case again, the file is read and parsed.
The former case is for compatibility with older perls, but makes no attempt to properly deal with character sets. If you open a file in a character-set-friendly way and then pass in a handle / object, then the method should Do The Right Thing as it only ever works with character data.
The parsefile function is so named because it is intended to work in a similar fashion to XML::Parser with the XML::Parser::EasyTree style. Instead of saying this:
use XML::Parser; use XML::Parser::EasyTree; $XML::Parser::EasyTree::Noempty=1; my $p=new XML::Parser(Style=>'EasyTree'); my $tree=$p->parsefile('something.xml');
you would say:
use XML::Tiny; my $tree = XML::Tiny::parsefile('something.xml');
Any document that can be parsed like that using XML::Tiny should produce identical results if you use the above example of how to use XML::Parser::EasyTree, with the exception that there is no support for attributes, and hence no 'attrib' key in hashes.
If you find a document where that is not the case, please report it as a bug.
The module is intended to be fully compatible with every version of perl back to and including 5.004_05, and may be compatible with even older versions of perl 5.
The lack of Unicode and friends in older perls means that XML::Tiny does nothing with character sets. If you have a document with a funny character set, then you will need to open the file in an appropriate mode using a character-set-friendly perl and pass the resulting file handle to the module.
The following parts of the XML standard are not handled at all or are handled incorrectly:
Not handled at all and ignored. However, a > character in CDATA or an attribute will make the primitive parser think the document is malformed.
This is not a validating parser.
In general, entities and references are not handled and so something like &65; will come through as the four characters &, 6, 5 and ;. Naked ampersand characters are allowed.
&65;
&
6
5
;
&, ', >, < and " are, however, supported.
&
'
>
<
"
These are ignored.
We do not guarantee to correctly handle leading and trailing whitespace.
I welcome feedback about my code, including constructive criticism. Bug reports should be made using http://rt.cpan.org/ or by email, and should include the smallest possible chunk of code, along with any necessary XML data, which demonstrates the bug. Ideally, this will be in the form of a file which I can drop in to the module's test suite. Please note that such files must work in perl 5.004_05, and that mishandling of funny character sets, even on later versions of perl, will not be considered a bug.
If you are feeling particularly generous you can encourage me in my open source endeavours by buying me something from my wishlist: http://www.cantrell.org.uk/david/wishlist/
XML::Parser
XML::Parser::EasyTree
http://beta.nntp.perl.org/group/perl.datetime/2007/01/msg6584.html
David Cantrell <david@cantrell.org.uk>
Copyright 2007 David Cantrell
This module is free-as-in-speech software, and may be used, distributed, and modified under the same terms as Perl itself.
This module is also free-as-in-mason software.
To install XML::Tiny, copy and paste the appropriate command in to your terminal.
cpanm
cpanm XML::Tiny
CPAN shell
perl -MCPAN -e shell install XML::Tiny
For more information on module installation, please visit the detailed CPAN module installation guide.