XML:Simple/XMLout converting elements to attributes



Below is customers.xml file



<?xml version="1.0"?>
<customers version="3.5" timestamp="2002-05-13 15:33:45">
<client identifier="62520">
<surname>Williams</surname>
<name>John</name>
<address>
<street>17 Liberty Ave.</street>
<locality>Birmingham</locality>
<province>Birmingham</province>
<zip>82649</zip>
</address>
<email>john.williams@expensive-mail.org</email>
<age>42</age>
</client>
<client identifier="62521">
<surname>Hightower</surname>
<name>Helen</name>
<address>
<street>2 Flying Saucer</street>
<locality>Southampton</locality>
<province>Southampton</province>
<cp>28001</cp>
</address>
<email>elerovw@cyb.org</email>
<age>37</age>
</client>
</customers>


And below is my perl code



use XML::Simple;
use Data::Dumper;

my $xml=XML::Simple -> new(KeepRoot => 1);
my $data=$xml->XMLin("customers.xml");
print Dumper($data);

$data->{'customers'}->{'client'}->{'John'}->{'address'}->{'zip'}="500011";
$data->{'customers'}->{'client'}->{'John'}->{'address'}->{'street'}="Prog colony";
$data->{'customers'}->{'client'}->{'John'}->{'address'}->{'province'}="A.P";
$data->{'customers'}->{'client'}->{'John'}->{'address'}->{'locality'}="Secbad";
$data->{'customers'}->{'client'}->{'John'}->{'address'}->{'Capital'}="Hyd";

print "\n";
$out=$xml->XMLout($data,OutputFile => "customers.xml");
print Dumper($out);


Below is the contents of the customers.xml after executing the above perl code.



<customers timestamp="2002-05-13 15:33:45" version="3.5">
<client name="Helen" age="37" email="elerovw@cyb.org" identifier="62521" surname="Hightower">
<address cp="28001" locality="Southampton" province="Southampton" street="2 Flying Saucer" />
</client>
<client name="John" age="42" email="john.williams@expensive-mail.org" identifier="62520" surname="Williams">
<address Capital="Hyd" locality="Secbad" province="A.P" street="Prog colony" zip="500011" />
</client>
</customers>


You see the elements are changed to attributes. I don't want to use NoAttr=>1. How to ensure that elements shown as elements and attributes are shown as attributes while using XMLout.


No comments:

Post a Comment