XML::Smart is out!
It's a smart, easy and powerful way to access/create XML data/files.
You access/create your XML data with a HASH tree style, but any point in the tree can be a HASH, ARRAY or SCALAR at the same time, so you don't need to care about the different types in the tree, you just us the tree and XML::Smart change it, or make it works like it, for you.
Download at:
http://search.cpan.org/author/GMPASSOS/XML-Smart-1.0/
Dependence:
http://search.cpan.org/author/GMPASSOS/Object-MultiType-0.01/
Here is an example of use:
use XML::Smart ;
my $XML = XML::Smart->new('file.xml') ;
## Change the root:
$XML = $XML->{hosts} ;
## Get the address [0] of server [0]:
my $srv0_addr0 = $XML->{server}[0]{address}[0] ;
## Or just:
my $srv0_addr0 = $XML->{server}{address} ;
## Get the server where the attibute 'type' eq 'suse':
my $server = $XML->{server}('type','eq','suse') ;
## Add a new server node:
my $newsrv = {
os => 'Linux' ,
type => 'Mandrake' ,
version => 8.9 ,
address => [qw(192.168.3.201 192.168.3.202)]
} ;
push(@{$XML->{server}} , $newsrv) ;
## Get/rebuild the XML data:
my $xmldata = $XML->data ;
## Save in some file:
$XML->save('newfile.xml') ;
## Send through a socket:
print $socket $XML->data(length => 1) ;
## show the 'length' in the XML header to the
## socket know the amount of data to read.
__DATA__
<?xml version="1.0" encoding="iso-8859-1"?>
<hosts>
<server os="linux" type="redhat" version="8.0">
<address>192.168.0.1</address>
<address>192.168.0.2</address>
</server>
<server os="linux" type="suse" version="7.0">
<address>192.168.1.10</address>
<address>192.168.1.20</address>
</server>
<server address="192.168.2.100" os="linux" type="conectiva" version=
+"9.0"/>
</hosts>
Graciliano M. P.
"The creativity is the expression of the liberty".
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|