Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Programmatic access of hashes

by metaperl (Curate)
on Jul 09, 2011 at 04:28 UTC ( #913481=note: print w/replies, xml ) Need Help??


in reply to Programmatic access of hashes

A hash is not an adequate structure for representing XML. XML can be order sensitive and Perl hashes are not. Also, the author of XML::Simple admits in his FAQ that even he doesnt process XML using his module --- it was only intended for XML as config files. It cant handle mixed content for instance.

On the other hand HTML::Element has a new_from_lol method that takes nested arrayrefs to form any type of XML possible.

Along those lines, I've developed code that compiles XML structure to the nested arrayref structure and uses Data::Diver to fill in the XML content with the hash values. Here a small sample of a file representing XML:

sub lol { my ($self) = @_; my $root = $self->data; [ QBXML => DIVE( $root, qw() ), [ QBXMLMsgsRq => { 'onError' => 'stopOnError' } => DIVE( $root, qw() ), [ CustomerAddRq => DIVE( $root, qw() ), [ CustomerAdd => DIVE( $root, qw() ), [ Name => DIVE( $root, qw(Name) ) ], [ IsActive => DIVE( $root, qw(IsActive) ) ], [ ParentRef => DIVE( $root, qw(ParentRef) ), [ ListID => DIVE( $root, qw(ParentRef ListID) +) ], [ FullName => DIVE( $root, qw(ParentRef FullNa +me) ) ] ], [ CompanyName => DIVE( $root, qw(CompanyName) ) ], [ Salutation => DIVE( $root, qw(Salutation) ) ], [ FirstName => DIVE( $root, qw(FirstName) ) ], [ MiddleName => DIVE( $root, qw(MiddleName) ) ], [ LastName => DIVE( $root, qw(LastName) ) ], [ BillAddress => DIVE( $root, qw(BillAddress) ), [ Addr1 => DIVE( $root, qw(BillAddress Addr1) +) ], [ Addr2 => DIVE( $root, qw(BillAddress Addr2) +) ], [ Addr3 => DIVE( $root, qw(BillAddress Addr3) +) ], [ Addr4 => DIVE( $root, qw(BillAddress Addr4) +) ], [ Addr5 => DIVE( $root, qw(BillAddress Addr5) +) ], [ City => DIVE( $root, qw(BillAddress City) ) +], [ State => DIVE( $root, qw(BillAddress State) +) ], [ PostalCode => DIVE( $root, qw(BillAddress PostalCode) +) ], [ Country => DIVE( $root, qw(BillAddress Count +ry) ) ], [ Note => DIVE( $root, qw(BillAddress Note) ) +] ], [ ShipAddress => DIVE( $root, qw(ShipAddress) ), [ Addr1 => DIVE( $root, qw(ShipAddress Addr1) +) ], [ Addr2 => DIVE( $root, qw(ShipAddress Addr2) +) ], [ Addr3 => DIVE( $root, qw(ShipAddress Addr3) +) ], [ Addr4 => DIVE( $root, qw(ShipAddress Addr4) +) ], [ Addr5 => DIVE( $root, qw(ShipAddress Addr5) +) ], [ City => DIVE( $root, qw(ShipAddress City) ) +], [ State => DIVE( $root, qw(ShipAddress State) +) ], [ PostalCode => DIVE( $root, qw(ShipAddress PostalCode) +) ], [ Country => DIVE( $root, qw(ShipAddress Count +ry) ) ], [ Note => DIVE( $root, qw(ShipAddress Note) ) +] ], [ Phone => DIVE( $root, qw(Phone) ) ], [ AltPhone => DIVE( $root, qw(AltPhone) ) ], [ Fax => DIVE( $root, qw(Fax) ) ], [ Email => DIVE( $root, qw(Email) ) ], [ Contact => DIVE( $root, qw(Contact) ) ], [ AltContact => DIVE( $root, qw(AltContact) ) ], ...




The mantra of every experienced web application developer is the same: thou shalt separate business logic from display. Ironically, almost all template engines allow violation of this separation principle, which is the very impetus for HTML template engine development.

-- Terence Parr, "Enforcing Strict Model View Separation in Template Engines"

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://913481]
help
Chatterbox?
[msh210]: About time... last time I was right was... I don't even remember when.
[shmem]: perl -e 'warn "test$/", ^@^ '
[Eily]: well, at least it's right about warn doing the same thing as die :)
[shmem]: same with undef as last element
[msh210]: oh, good point. It's the best kind of correct
[shmem]: msh210: but I'd not call it a lie outright. That's when you do know the truth.
[msh210]: I don't have sendmail (I'm on MS Windows), so does one of you think you can report the bug?
[shmem]: msh210: 'tis about time to tell the sad truth to perl5porters - via perlbug
[msh210]: probably whoever wrote that actually knows the truth
[shmem]: msh210: you can save the perlbug text in a file and send that via your mail agent of choice

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (15)
As of 2017-04-24 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (439 votes). Check out past polls.