Closed schwern closed 11 years ago
It's not entirely accurate to say I've "disowned" XML::Simple :-) It's more that I've recognised that the XML::Simple API is a) awful, b) not 'simple' and c) unsalvageable. So the module is in maintenance mode and won't get any new features.
Having said that, you don't need any new features, so something like this could work:
my $xml = XML::Simple::XMLout($data, KeyAttr => {}, NoAttr => 1, KeepRoot => 1);
The three options in that example are required to override annoying defaults. It will die if $data contains a blessed reference and may generate warnings for undef values.
I'd definitely recommend JSON support first. If someone really desperately needs XML support, they might have some useful perspective on the shortcomings of my 1-liner above.
Thanks for the note, @grantm. Any way to get that to not die if there's a blessed reference, just turn it into a hash? We don't worry about blessed things for JSON or YAML. And is there a way to make it encode undef usefully?
(We have JSON and YAML support already)
Yeah see this is why you can't make an XML API that's simple :-) XML has no concept of undefined, so it's up to each schema designer to decide how they'd like to encode the notion of a thing existing but not having a defined value. XML-RPC for example considers undefined and the empty string to be the same thing.
The two most straightforward options are to completely omit undefined values when serialising (set: SuppressEmpty => 1) or to output them as empty strings without a warning (set: SuppressEmpty => undef).
There is no way to have XMLout not die on blessed references. Presumably your JSON serialisation uses the allow_nonref option. So a workaround for XML might be to serialise to JSON and back using that option and then pass the result to XMLout().
On balance it might be better to not do XML serialisation at all. Providing XML serialisation will only encourage people to use XML and that in turn will result in people emailing you to tell you you're doing it wrong. JSON rules!!
If you really want to do XML, XML::Dumper is a module that's designed for the job. I've never used it.
So what you're saying is its complicated. :)
XML::Dumper looks easy, but the output doesn't look particularly useful.
I think there's a cognitive dissonance here between JSON, YAML and XML. JSON and YAML are data serialization languages, you can dump things and read them out without much fuss or a schema. They work well for serializing any given structure.
You wouldn't use XML for random data serialization, you use it for generating very specific documents. In that sense, this doesn't make any sense so I'm going to close it up.
Add a method to dump as XML.
$obj->mo->dump( format => "xml" )
. After #230 add$obj->mo->as_xml
.The first question is how do we dump a Perl object as XML? There's so many ways to do it. The basic problem of how do you translate hash/list/scalar into a tree structure? Maybe we won't dump "as XML" but as a particular module does it. "as XML::Simple" for example. This also makes it easier to have options specific to a XML module without having to generalize them.
My impulse is to grab a popular module and use that. XML::Simple is the first thing to hand, but I have it on rumor that @grantm has disowned that module. Grant, care to comment?
PS I know pretty much nothing practical about XML.