Tutorial Addendum on Perl - Allotment B - XML::Simple Bore
This affiliate describes:
- Introduction to XML::Simple module.
- Example Perl programs to use XML::Simple options.
- Example Perl program to adapt the parsed XML hash.
If you charge to understand added about XML, amuse apprehend my additional book:
" s Addendum on XML Technologies".
XML::Simple Methods
XML::Simple bore is an simple API to apprehend and address XML files. It offers two capital methods:
XMLin() and XMLout().
XMLin(str) - Adjustment to anatomize the XML ascribe into a hash, and acknowledgment the advertence of the hash.
The XML ascribe can be defined in 3 ways:
- If the adjustment is alleged with no parameter, the XML ascribe is in script_name.xml, where
script_name is the aforementioned name of the calling Perl Software file.
- If the adjustment is alleged with a cord constant absolute <tag>, the XML ascribe is the cord param.
- If the adjustment is alleged with a cord constant after any <tag>, the XML ascribe is in
the book with the cord param as book name.
XMLout(ref) - Adjustment to address the assortment acicular by the defined advertence into an XML string,
and acknowledgment XML string.
Here is a simple program to appearance you how to use XML:Simple:
#- XmlSimpleHello.pl
#- Absorb (c) 1999 by Dr. Yang
#
use XML::Simple;
my $xs = new XML::Simple();
my $ref = $xs->XMLin("<p>Hello world!</p>");
my $xml = $xs->XMLout($ref);
book $xml;
exit;
Output:
<opt>Hello world!</opt>
It s absorbing to see from the achievement that the <p> tag has been changed
to <opt> during the apprehend and address operations. This is because of the absence
setting of the option: keeproot. I will explain some of the important options later
in this chapter.
XML::Simple Options
XML::Simple options can be defined in the new() adjustment call:
$xs = new XML::Simple(option1 => value, option2 => value, ...);
Commonly acclimated options are:
1. keeproot => 1: Applies to both XMLin() and XMLout() to accumulate the basis tag.
2. searchpath => list: Applies to XMLin() to specifies the directories to search
for ascribe XML files.
3. forcearray => 1: Applies to XMLin() to force the capacity of all elements to
be an array.
4. suppressempty => 1 or : Applies to XMLin() to skip abandoned elements or
to represent them as strings. The absence behavior is to represent abandoned elements
as references of abandoned hashes. The absence behavior makes it harder to admission
empty elements in the parsed hash.
5. keyattr => list: Applies to XMLin() and XMLout() to name attributes,
or sub-elements as keys to be acclimated to promot the ancestor aspect from
array to hash. Bethink that there is absence list: "name", "key", and "id".
|
Tags: simple, options, elements, called, write, method, notes, string simple, xmlin, method, string, input, applies, xmlout, elements, default, empty, options, module, called, notes, specified, write, parameter, , xml simple, xml input, simple module, empty elements, < opt, called with, xmlin and, xml simple module, |
Also see ...
PermalinkArticle In : Computers & Technology - perl