Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: comments on xml 2 hash 2 xml using libXML

by space_monk (Chaplain)
on Mar 18, 2013 at 14:29 UTC ( #1024065=note: print w/ replies, xml ) Need Help??


in reply to comments on xml 2 hash 2 xml using libXML

I found this when I was browsing for a similar function myself, so I'm sorry for arriving late to the party! :-) I'm just posting this in case anyone else is looking for a similar answer.

There are a couple of CPAN modules which appear to do a similar job. Specifically XML::Hash and XML::Hash::LX.

Both modules predate the 2012 date of the question by a couple of years.

A Monk aims to give answers to those who have none, and to learn from those who know more.


Comment on Re: comments on xml 2 hash 2 xml using libXML
Re^2: comments on xml 2 hash 2 xml using libXML
by Anonymous Monk on Mar 18, 2013 at 15:32 UTC
    How do they compare?

      I'll let you know in a few weeks time, as I'll probably be evaluating them for use in the project I'm working on. Its only my first day in the office today....

      Update: I was going to post a request for help because I couldn't get XML::Hash::LX to build in Cygwin; however installing "make" seemed to fix that one.... :-P

      A Monk aims to give answers to those who have none, and to learn from those who know more.
Re^2: comments on xml 2 hash 2 xml using libXML
by tobyink (Abbot) on Mar 18, 2013 at 16:44 UTC

    No module that aims to convert between XML and hashrefs does a good job. This is simply because the data model of XML is nothing like a hash.

    How do you represent this as a hash?

    <root toot="1"> 2 <toot>3</toot> 4 <toot>5</toot> 6 </root>

    You either end up with a hopelessly complicated hash/array structure to represent it unambiguously:

    { root => { attributes => { toot => 1, }, contents => [ 2, { toot => { contents => [3] } }, 4, { toot => { contents => [5] } }, 6 ], } }

    ... which is a nightmare to find stuff in. Or you do this:

    { name => "root", toot => [1,3,5], text => [2,4,6], }

    ... and stop caring about stuff like the distinction between attributes and elements and text nodes, the order of a node's children, etc... which means that when you start outputting XML, the XML you generate will confuse the hell out of any tools that consume it.

    Which is not to say that particular flavours of XML - e.g. RSS or Atom or OPML or blah or blah - cannot be usefully converted to a hash by a module that understands the schema. If you know that an Atom <entry> element can never validly have a title attribute, but will always have exactly one <title> element as a child, then representing that as:

    my $entry = { title => "...", ..., };

    ... is fine. But modules like XML::Simple and its ilk don't target specific flavours of XML; they try to handle generic XML.

    Even the very best generic XML-to-hash module will be horribly broken, because the whole concept is horribly broken.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      Yes, fortunately I'm going from a hash to XML, but recognise that it will have issues.....

      A Monk aims to give answers to those who have none, and to learn from those who know more.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (13)
As of 2014-04-18 16:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (470 votes), past polls