Perl read-write XML

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.