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!