Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

XML::Simple - attribute encoding, order of XML

by metaperl (Curate)
on Aug 17, 2009 at 06:24 UTC ( [id://789084]=note: print w/replies, xml ) Need Help??


in reply to Re: A data structure for XML generation
in thread A data structure for XML generation

Actually XML::Simple has a very simple rule for determining whether to output something as an attribute vs a nested element. If the value is a plain scalar, it will be output as an element, otherwise it will be a nested element.
Indeed... Just FYI, a co-worker and myself were evaluating XML::Simple for producing XML and he had the impression that there was no way to do this. If you glance at the SYNOPSIS, and see this:
'gobi' => { 'osversion' => '6.5', 'osname' => 'irix', 'address' => '10.0.0.102' },
you get the idea that there is no way to force address to be an element somehow.

I knew about ForceArray but for some reason, things didn't click for me until your post here.

regexps as "non-plain scalars"

I wanted to ease my typing. The standard "non-plain scalar" is this:
mother => ['Mary']
but I was hoping to get away with this:
mother => qr/Mary/
because it is easier to type and more readable (IMHO).

Any chance of converting a node whose value is a regexp into an element as opposed to an attribute?

Order of sibling XML elements

Now, one other question, we are producing XML based on an XSD. I am wondering if the XML has order requirements on siblings. In other words, I did the sample XML for the original topic in this thread in XML::Simple -
use strict; use warnings; use XML::Simple; use Tie::IxHash; tie (my %struct, 'Tie::IxHash', family => { name => 'Kawasaki', father => ['Yasuhisa'], mother => ['Chizuko'], children => { girl => ['Shiori'], boy => ['Kairi'], } } ); warn XMLout(\%struct);
but while each level of the XML tree was accurate, the sibling elements father, mother, children were in a different order than I wanted them to be.

Replies are listed 'Best First'.
Re: XML::Simple - attribute encoding, order of XML
by grantm (Parson) on Aug 17, 2009 at 21:04 UTC
    The short answer is that XML::Simple is absolutely not the right module for generating XML. I don't generate XML very often and when I do I tend to use a templating tool like TT or Mason.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://789084]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2025-11-10 20:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (66 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.