Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

In reply to Re^2: comments on xml 2 hash 2 xml using libXML by tobyink
in thread comments on xml 2 hash 2 xml using libXML by irishBatman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-24 01:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found