GH_perler has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, when transfering a simple data structure with XML::Simple xmlout I got an unwanted behaviour by XML::Simple.
The basic hash as simple as it can be:

my %xmlData; $xmlData{'Date'}='2018-10-15'; $xmlData{'Time'}='18:20:00'; $xmlData{'Depot'}{'Account'}{'Consignment'}{'Type'} = 'D'; $xmlData{'Depot'}{'Account'}{'Consignment'}{'ImportID'} = '123; $xmlData{'Depot'}{'Account'}{'Consignment'}{'Reference'} = 'RefNo +'; $xmlData{'Depot'}{'Account'}{'Consignment'}{'Number'} = 'ShipN +o'; $xmlData{'Depot'}{'Account'}{'Consignment'}{'Lifts'} = '3'; $xmlData{'Depot'}{'Account'}{'Consignment'}{'Weight'} = '1800' +; $xmlData{'Depot'}{'Account'}{'Consignment'}{'ManifestNote'} = 'Freig +ht to load'; $xmlData{'Depot'}{'Account'}{'Consignment'}{'CollectionDate'} = '2018- +10-15'; $xmlData{'Depot'}{'Account'}{'Consignment'}{'DeliveryDate'} = '2018- +10-17'; $xmlData{'Depot'}{'Account'}{'Consignment'}{'DeliveryTime'} = '15:00 +:00'; [...] Code between [...] $xmlData{'Depot'}{'Account'}{'Consignment'}{'Pallet'}='PID_987654'; $xmlData{'Depot'}{'Account'}{'Consignment'}{'BillUnit'}{'Type'}='QP'? $xmlData{'Depot'}{'Account'}{'Consignment'}{'BillUnit'}{'Amount'}='3';

Using this code I get mostly what I want, but unfortunately not at all

my $xml = new XML::Simple; my $xmlout = $xml -> XMLout(\%xmlData, NoAttr => 1, RootName => 'Manif +est', KeyAttr => 'Account', NoSort => 1, SuppressEmpty => undef ); $xmlout = '<?xml version="1.0" encoding="UTF-8"?>'."\n".$xmlout;

XML Result is like:

<?xml version="1.0" encoding="UTF-8"?> <Manifest> <Depot> <name>Account</name> <Consignment> <Address> <Town>[...]</Town> <Type>[...]</Type> <Line>[...]</Line> <Country>[...]</Country> <County>[...]</County> <CompanyName>[...]</CompanyName> <ContactName>[...]</ContactName> <PostCode>[...]</PostCode> <Telephone>[...]</Telephone> </Address> <Address> <CompanyName>[...]</CompanyName> <PostCode>[...]</PostCode> <Town>[...]</Town> <Country>[...]</Country> <Type>[...]</Type> <Line>[...]</Line> </Address> <Lifts>[...]</Lifts> <Number>[...]</Number> <CollectionDate>[...]</CollectionDate> <Pallet>[...]</Pallet> <Reference>[...]</Reference> <BillUnit> <Type>[...]</Type> <Amount>[...]</Amount> </BillUnit> <Service> <ServiceGroupCode>[...]</ServiceGroupCode> <ServiceCode>[...]</ServiceCode> <Type>[...]</Type> </Service> <Weight>[...]</Weight> <DeliveryDate>[...]</DeliveryDate> <ImportID>[...]</ImportID> <ManifestNote>[...]</ManifestNote> <Type>[...]</Type> <DeliveryTime>[...]</DeliveryTime> </Consignment> </Depot> <Date>[...]</Date> <Time>[...]</Time> </Manifest>

The problem ist, that I donīt get <Account>..</Account> as wrapper of <consignment>...</consignment> but <name>Account</name> (when using SuppressEmpty => undef) or <Account>Account</Account> (when not using SuppressEmpty => undef).

Does anyone of you know why or what to configure to get the result I need?

Thanks in advance
George

Replies are listed 'Best First'.
Re: XML::Simple Data->xml element conversion problem
by Discipulus (Canon) on Oct 15, 2018 at 16:52 UTC
    Hello GH_perler and welcome to the monastery and to the wonderful world of Perl!

    the module is deprecated, I use XML::Twig for XML but there are various options, see:

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: XML::Simple Data->xml element conversion problem
by poj (Abbot) on Oct 15, 2018 at 18:02 UTC
    Does anyone of you know why or what to configure to get the result I need?
    KeyAttr => [],

    see XML::Simple section KeyAttr => [ list ] # in+out - important

    Note 1: The default value for 'KeyAttr' is ['name', 'key', 'id']. If you do not want folding on input or unfolding on output you must set this option to an empty list to disable the feature.
    
    poj
Re: XML::Simple Data->xml element conversion problem
by holli (Abbot) on Oct 17, 2018 at 01:09 UTC
    Back in the day I did my share of XML-processing and if I learned anything then that all modules suck at outputting XML. Either you run into encoding issues, or something chokes on namespaces or whatnot.
    Instead I looked at the problem a different way: I have data in memory. I want a textual representation of it. I use templates. That also decouples output from code which can be a good thing. You have one more file to handle though.


    holli

    You can lead your users to water, but alas, you cannot drown them.