Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Recomendations for a XML writer module

by salva (Canon)
on Jul 13, 2017 at 10:43 UTC ( #1195009=perlquestion: print w/replies, xml ) Need Help??

salva has asked for the wisdom of the Perl Monks concerning the following question:

I need to generate XML files from Perl and I would like to get some module recommendations. Some years ago I would stick to XML::LibXML, but well, maybe there is something better now!

Replies are listed 'Best First'.
Re: Recomendations for a XML writer module
by choroba (Archbishop) on Jul 13, 2017 at 11:12 UTC
    My favourite tool for processing and creating XML is XML::XSH2 , which I also accidentally maintain. Its interactive shell is great for experimenting with XPath expressions and more complex structures. It's a wrapper around XML::LibXML , so if speed is a concern (alas, xsh is slow), it's possible to easily rewrite the whole thing into XML::LibXML , but I find XML::LibXML too verbose, and doubly so for creating XML, so in the end, I usually use Template or some other templating system available.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Recomendations for a XML writer module
by Corion (Pope) on Jul 13, 2017 at 12:18 UTC

    If you have an XSD for your XML then XML::Compile can take care for creating well-formed XML from your Perl data structures.

Re: Recomendations for a XML writer module
by Discipulus (Abbot) on Jul 13, 2017 at 11:24 UTC
Re: Recomendations for a XML writer module
by hippo (Chancellor) on Jul 13, 2017 at 11:00 UTC

    Luckily enough I rarely have to deal with XML these days. However, a quick look at Task::Kensho::XML shows that XML::LibXML still features among the recommended list. There are a couple of specifically generator/writer modules in there too which you could try.

Re: Recomendations for a XML writer module
by runrig (Abbot) on Jul 13, 2017 at 22:06 UTC
    Nobody's mentioned XML::Writer yet, so I will. It does what it says it does, and does it well, and simply.
Re: Recomendations for a XML writer module
by salva (Canon) on Jul 17, 2017 at 09:20 UTC
    After considering all the alternatives proposed it seems I should still stick to XML::LibXML:

    XML::XSH2 as suggested by choroba seems interesting, but it is a language of its own, and overkill for the task at hand that is to generate a XML document from a Perl data structure.

    Using a templating module as Template doesn't seem like a good option in my case either; my structure is deeply recursive which would make the template complex. Besides that, I don't want to think about escaping data and whether I am generating correct XML.

    XML::Compile, seems also overkill in this case. The schema is quite big I am only going to use a very small subset.

    XML::Writer, seems right for the job but I don't like how it works, keeping an implicit tag open and manipulating it.

    XML::Twig and probably XML::Rules, are excellent modules for parsing XML.

    So, I am doing as haukex and using XML::LibXML with a couple of helper subs, to get rid of its verbosity (update: Soon to be available from CPAN as XML::FromPerl!)

Re: Recomendations for a XML writer module
by sleet (Sexton) on Nov 03, 2017 at 18:13 UTC
    Dave Wheeler has a thorough discussion on his blog post from 2009: Generating XML in Perl. Make sure to also read the comments on the post.
Re: Recomendations for a XML writer module
by sundialsvc4 (Abbot) on Jul 13, 2017 at 13:16 UTC

    I tend to fall back on either XML::LibXML or XML::Twig, depending on exactly what I am doing, and how enormous the XML file is.   Twig is good for processing a great big file incrementally – it is a parser that can call your code at strategic points, but which does not transform the XML file structure into an in-memory structure.   XML::LibXML’s advantage is that under the hood it leverages libxml2, which is an industry-standard binary that I think is the actual source of most of the XML files that you are likely to receive.   So, if compatibility with a distant planet is an important consideration (and if you want to pre-flight your files to be sure that they will be acceptable), this is a good way to go.   This binary library implements everything associated with XML processing, in an industry-standard way.

    “Handwritten” XML is also seen a lot, especially when you know that it will not change or when one in-house app is talking to another, but there was a recent thread here where someone seems to have gotten bitten by “character sets” with such a program.   (Switch from Latin1 to UTF-8.)   XML read/writers will categorically take care of all that, if used correctly.

    The third most-common contender, XML::Simple, is a little too simple for my taste.   I have experienced various problems with both reading and writing if the use-case gets anywhere close to being an edge-case.   (However, it is simple, and sometimes functional XML requirements are simple, too.)

      XML::Simple ... it is simple, and sometimes functional XML requirements are simple, too

      Sorry, but I have to strongly recommend against XML::Simple for any XML output, and don't even think about trying to round-trip XML files with XML::Simple. Reading simple XML files with the module might be acceptable if and only if the user is aware of all of the issues, but most people who come here to ask for help with XML::Simple are clearly not aware of those issues, and that's a problem with that module. Despite the fact that I really like the idea behind the module, unfortunately XML::Simple's output format is just too brittle and most XML schemas are too strict for the module to be a reliable XML writer.

      XML::Rules is a pretty good replacement for XML::Simple for reading XML files into data structures, but I've noticed it's also still not always able to perfectly round-trip XML files. Personally, for writing XML files, I usually find myself using XML::LibXML (despite the relative verbosity of its API, since a few short helper subs take care of that), and for reading or filtering XML files, especially large ones, XML::Twig.

      FYI:

      "The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces. In particular, XML::LibXML is highly recommended and XML::Twig is an excellent alternative."

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2021-05-07 05:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (87 votes). Check out past polls.

    Notices?