Perl reads and writes XML: The main is to write always forget how to engage ...
first-read XML
1) XML instance
<?xml version= "1.0" encoding= "UTF-8"?>
<employees>
<employee age = "> "
<name>linux</name>
<country>US</country>
</employee>
<employee age = "Ten" >
<name>mac</name>
<country>US</country>
</employee>
<employee age = "> "
<name>windows</name>
<country>US</country>
</employee>
</employees>
2) Code
Use file::basename;
Use xml::simple;
Use Data::D umper;
My $xmlfile = DirName ($). "\\employees.xml";
if (-e $xmlfile)
{
print "-----------------------------------------\ n";
My $userxs = xml::simple->new (keyattr = "name");
my $userxml = $userxs->xmlin ($xmlfile);
# Print Output
print dumper ($userxml);
my%allemployees =%{$userxml->{employee}};
foreach my $key (keys (%allemployees))
{
print $key. " ";
print $allemployees {$key} {"Age"}. "\ n";
}
print "-----------------------------------------\ n";
My $userxsT = xml::simple->new (Forcearray = 1);
my $userxmlT = $userxsT->xmlin ($xmlfile);
# Print Output
print dumper ($userxmlT);
my @allemployeeT = @{$userxmlT->{"Employee"}};
foreach my $employee (@allemployeeT)
{
print $employee->{"name"}[0]. " ";
print $employee->{"age"}. "\ n";
}
}
3) Results
two write XML
Code:
Use file::basename;
Use xml::simple;
Use Data::D umper;
print "-----------------------------------------\ n";
# Create Array
my @arr = [
{' Country ' = ' England ', ' capital ' = ' london '},
{' Country ' = ' Norway ', ' capital ' = ' Oslo '},
{' Country ' = ' India ', ' capital ' = ' New Delhi '}];
# Create Object
my $xml = new Xml::simple (noattr=>1, rootname=> ' dataroot ');
# convert Perl array ref into XML document
my $data = $xml->xmlout (\ @arr, outputfile = "Output1.xml");
print "-----------------------------------------\ n";
my $str =<<_xml_string_;
<config logdir= "/var/log/foo/" debugfile= "/tmp/foo.debug" >
<server name= "Sahara" Osname= "Solaris" osversion= "2.6" >
<address>10.0.0.101</address>
<address>10.0.1.101</address>
</server>
<server name= "Gobi" osname= "Irix " osversion= "6.5" >
<address>10.0.0.102</address>
</server>
<server name= "Kalahari" Osname= "Linux" osversion= "2.0.34" >
<address>10.0.0.103</address>
<address>10.0.1.103</address>
</server>
</config>
_xml_string_
my $xml _ref=xmlin ($str, keeproot = 1);
my $xml _str=xmlout ($xml _ref,outputfile = "Output2.xml");
print "-----------------------------------------\ n";
three more
Common parameters:
keyattr = [List] # in+out-important or keyattr = {List} # In+out-important represents the key in the read-out Dict.
Forcearray = 1 # in-important or forcearray = [names] # in-important means that the child element in the dict is represented as an array, not dict.
noattr = 1 # In+out-handy Indicates whether the child elements and attributes are written as properties and whether the properties are ignored when read.
rootname = ' String ' # Out-handy represents the root node name when writing XML.
keeproot = 1 # In+out-handy indicates that the root node is processed
xml-simple Module Detailed reference: HTTP://SEARCH.CPAN.ORG/~GRANTM/XML-SIMPLE-2.18/LIB/XML/SIMPLE.PM
Finish it!
Thanks, thanks!.
Perl read-write XML