John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

I want to save my data as an XML file. XML::Simple doesn't have a clue as to my schema, though it might be fine for reading it in. Writing out is easy, just traverse it and print stuff. But I'm not about to write functions for correctly emitting elements and escaping out special characters and whatnot, because Perl should have that stuff already.

But, looking at the library, I see XML::Parser but no sign of going the other direction.

What module should I look for?

--John

Replies are listed 'Best First'.
Re: Which module to use for writing XML file?
by merlyn (Sage) on Mar 25, 2006 at 06:32 UTC
Re: Which module to use for writing XML file?
by idsfa (Vicar) on Mar 25, 2006 at 06:31 UTC

    XML::Dumper, XML::Writer, XML::Writer::Simple ...

    # example using XML::Dumper use XML::Dumper; my $dump = new XML::Dumper; my $perl = ( # some perl data structure ); $dump->dtd( $file, $url ); my $xml_with_link_to_dtd = $dump->pl2xml( $perl ); # example using XML::Writer::Simple use XML::Writer::Simple dtd => "file.dtd"; print para("foo",b("bar"),"zbr"); # Asuming para. etc are defined in the DTD

    XML::Dumper has the advantage of providing both read and write interfaces ...


    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon
Re: Which module to use for writing XML file?
by wazoox (Prior) on Mar 25, 2006 at 10:20 UTC
    After struggling for a while with XML::Simple I finally found XML::LibXML quite easy to work with. It's somewhat low level, but at least it does exactly what you mean.
    For instance, this simple sub builds XML from a hash in my latest application :

    updated : added readmore tags. In case you're wondering, the documentation is in ROBODoc format.

Re: Which module to use for writing XML file?
by Cody Pendant (Prior) on Mar 25, 2006 at 08:45 UTC
    I'm sure everyone else's posts will help you, but it's worth nothing that practically everyone has trouble with XML::Simple the first time they try it, especially with writing out of data, but it will do what you want if you spend a bit of time tweaking and understand how it works. There are lots of posts here if you search, and the POD for XML::Simple is pretty good.


    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print
Re: Which module to use for writing XML file?
by grantm (Parson) on Mar 27, 2006 at 03:26 UTC