in reply to Reading structures from XML

I think the problem is that there just isn't a 1:1 way to do this. This is why XML::Simple gets so hairy. Consider-

<container> <thing foo="bar">baz</thing> <thing qux="oop" foo=""/> <thing/> </container>

What could that look like in a Perl structure? It would have to have objects mimicking DOM or something like "content/children" named placeholders.

So, I don't think this is easy to solve and it will probably be messy or just a different API to the DOM that's almost as complicated. I'd love to be proved wrong in this case. :) I'm an XML::LibXML user that also would love having straight mapping to Perl structures now and then.

Replies are listed 'Best First'.
Re^2: Reading structures from XML
by BrowserUk (Patriarch) on May 07, 2009 at 01:13 UTC

      No, I think I was the one missing something. That's pretty nice. I hadn't even considered using an empty hash key to point to the content. It's a nice in to account for what I'd seen as semi-intractable.

Re^2: Reading structures from XML
by John M. Dlugosz (Monsignor) on May 06, 2009 at 20:56 UTC
    That's my point — it is not simple nor automatic. In Perl, you design Perl datastructures or object constructors and subsequent manipulators. In XML, you design a good XML-ish way of doing it. .NET tries to make you think the in-program structure and the XML are one and the same, but the design issues are different, and you end up with a lame serialization of the in-program data, not a proper XML-mindset design.

    So, in general, have the user write a function that takes the Element and return the Perl object. But, have a nifty library of things to make doing that easy.

    I agree your example really needs a general DOM. But most real program designed to act on information read from an XML file will have data designed for that purpose; e.g. it will be sane and meaningful.

    —John