- Prison Code Breaker Diary -

=> aka: Nhật Kí Code Tù

Categories

Language: Perl
Presiquisite: XML::Simple

Assuming we have a xml file like this: data.xml

<?xml version="1.0" encoding="UTF-8"?>
<database>
<user>
<uname>Pete Houston</uname>
<upass>123456</upass>
<email>pete.houston.17187@gmail.com</email>
<id>1</id>
</user>
<user>
<uname>Bach Van Kim</uname>
<upass>987654</upass>
<email>bvkvn06@gmail.com</email>
<id>2</id>
</user>
</database>
We will dump this XML tree using XML::Simple and Dumper package:

#!C:\perl\bin\perl.exe -w
use strict;
use XML::Simple;
use Data::Dumper;

print Dumper( XML::Simple->new()->XMLin("data.xml"));

The result shows:

$VAR1 = {
'user' => {
'1' => {
'email' => 'pete.houston.17187@gmail.com',
'uname' => 'Pete Houston',
'upass' => '123456'
},
'2' => {
'email' => 'bvkvn06@gmail.com',
'uname' => 'Bach Van Kim',
'upass' => '987654'
}
}
};

The method: XML::Simple->new() will create an object to handle XML file, and then input file will be called through method XMLin("filename.xml");
A very simple way!

0 comments

Post a Comment