NAME
XML::Tiny::Tree - Convert XML::Tiny output into a Tree
Synopsis
This is scripts/synopsis.pl:
#!/usr/bin/env perl
use strict;
use warnings;
use XML::Tiny::Tree;
# ------------------------------------------------
my($input_file) = shift || die "Usage $0 file. Try using data/test.xml as the input. \n";
my($tree) = XML::Tiny::Tree -> new
(
input_file => $input_file,
no_entity_parsing => 1,
) -> convert;
print "Input file: $input_file. \n";
print "The whole tree: \n";
print map("$_\n", @{$tree -> tree2string});
print '-' x 50, "\n";
print "Bits and pieces from the first child (tag_4) of the second child (tag_3) of the root (tag_1): \n";
my(@children) = $tree -> children;
@children = $children[1] -> children;
my($tag) = $children[0] -> value;
my($meta) = $children[0] -> meta;
my($attr) = $$meta{attributes};
print "tag: $tag. \n";
print "content: $$meta{content}. \n";
print 'attributes: ', join(', ', map{"$_ => $$attr{$_}"} sort keys %$attr), ". \n";
Description
XML::Tiny::Tree reads a file via XML::Tiny, and reformats the output into a tree managed by Tree.
Constructor and Initialization
Calling new()
new()
is called as my($obj) = XML::Tiny::Tree -> new(k1 => v1, k2 => v2, ...)
.
It returns a new object of type XML::Tiny::Tree
.
Key-value pairs accepted in the parameter list (see corresponding methods for details [e.g. "input_file([$string])"]):
- o fatal_declarations => $Boolean
-
Specify whether or not to get XML::Tiny to error if such declarations are found.
Default: 0.
This key is optional.
- o input_file => $string
-
Specify the name of the XML file to process.
Default: ''.
This key is mandatory.
- o no_entity_parsing => $Boolean
-
Specify whether or not to get XML::Tiny to do entity parsing.
Default: 0.
This key is optional.
- o strict_entity_parsing => $Boolean
-
If set to true, any unrecognised entities (ie, those outside the core five plus numeric entities) cause a fatal error.
Default: 0.
This key is optional.
Distributions
This module is available as a Unix-style distro (*.tgz).
See http://savage.net.au/Perl-modules/html/installing-a-module.html for help on unpacking and installing distros.
Installing the module
Install XML::Tiny::Tree as you would for any Perl
module:
Run:
cpanm XML::Tiny::Tree
or run:
sudo cpan XML::Tiny::Tree
or unpack the distro, and then either:
perl Build.PL
./Build
./Build test
sudo ./Build install
or:
perl Makefile.PL
make (or dmake)
make test
make install
Methods
convert([%arg])
Here, the [] indicate an optional parameter.
Triggers reading the XML file and conversion of the output of XML::Tiny into a Tree.
Returns an object of type Tree.
convert()
takes the same parameters as "new([%arg])". See "Constructor and Initialization" for details.
fatal_declarations([$Boolean])
Here, the [] indicate an optional parameter.
Gets or sets the value of the option to pass to XML::Tiny.
fatal_declarations
is a parameter to new([%arg]).
input_file([$string])
Here, the [] indicate an optional parameter.
Gets or sets the name of the input file to pass to XML::Tiny's method parsefile()
.
input_file
is a parameter to new([%arg]).
new([%arg])
See "Constructor and Initialization" for details.
no_entity_parsing([$Boolean])
Here, the [] indicate an optional parameter.
Gets or sets the value of the option to pass to XML::Tiny.
no_entity_parsing
is a parameter to new([%arg]).
strict_entity_parsing([$Boolean])
Here, the [] indicate an optional parameter.
Gets or sets the value of the option to pass to XML::Tiny.
strict_entity_parsing
is a parameter to new([%arg]).
FAQ
How to I access the names of the XML tags?
Each node in the tree is an object of type Tree, and has a method called value()
. This method returns a string which is the name of the tag.
See the "Synopsis" for sample code.
How do I access the attributes of each XML tag?
Each node in the tree is an object of type Tree, and has a method called meta()
. This method returns a hashref containing 2 keys:
If the tag has no attributes, then $hashref is {}.
If the tag has no content, then $string is ''.
See the "Synopsis" for sample code.
How do I access the content of each XML tag?
See the answer to the previous question.
See the "Synopsis" for sample code.
Is it possible for a tag to have both content and sub-tags?
Yes. See data/test.xml in the distro for such a case.
See Also
Machine-Readable Change Log
The file Changes was converted into Changelog.ini by Module::Metadata::Changes.
Version Numbers
Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions.
Repository
https://github.com/ronsavage/XML-Tiny-Tree.
Support
Email the author, or log a bug on RT:
https://rt.cpan.org/Public/Dist/Display.html?Name=XML-Tiny-Tree.
Author
XML::Tiny::Tree was written by Ron Savage <ron@savage.net.au> in 2015.
Home page: http://savage.net.au/index.html.
Copyright
Australian copyright (c) 2015, Ron Savage.
All Programs of mine are 'OSI Certified Open Source Software';
you can redistribute them and/or modify them under the terms of
The Artistic License, a copy of which is available at:
http://www.opensource.org/licenses/index.html