Perl read/write XML

Source: Internet
Author: User

one-read XML
1) XML instance



Linux
us


Mac
us


Windows
us


2) Code
Use File: basename;
use XML: simple;
Use Data: dumper;

my $ xmlfile = dirname ($0 ). "\ employees. XML ";
If (-e $ xmlfile)
{< br> 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)
{< br> 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

 
Write XML
Code:
Use File: basename;
Use XML: simple;
Use Data: dumper;

Print "----------------------------------------- \ n ";
# Create Array
My @ arr = [
{'Country' => 'England ', 'capital' => 'London '},
{'Country' => 'norway', 'capital '=> 'osslo '},
{'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 _;


10.0.0.101
10.0.1.101


10.0.0.102


10.0.0.103
10.0.1.103


_ xml_string _

my $ xml_ref = xmlin ($ STR, keeproot => 1);
my $ xml_str = xmlout ($ xml_ref, outputfile => "output2.xml ");
Print "--------------------------------------- \ n";
3 more
common parameters:
keyattr => [LIST] # In + out-important or keyattr => {list} # In + out-important indicates the key in the read dict.
forcearray => 1 # In-important or forcearray => [names] # In-important indicates that child elements in dict are represented as arrays rather than dict.
noattr => 1 # In + out-handy indicates whether to write sub-elements and attributes as attributes, and whether to ignore the attributes during reading.
rootname => 'string' # Out-handy indicates the name of the root node when writing XML.
keeproot => 1 # In + out-handy indicates processing to get the root node
XML-simple module detailed reference: http://search.cpan.org /~ Grantm/XML-Simple-2.18/lib/XML/simple. PM
finished!
thanks, thanks!

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.