29

I need to create XML in Perl. From what I read, XML::LibXML is great for parsing and using XML that comes from somewhere else. Does anyone have any suggestions for an XML Writer? Is XML::Writer still maintained? Does anyone like/use it?

In addition to feature-completeness, I am interested an easy-to-use syntax, so please describe the syntax and any other reasons why you like that module in your answer.

Please respond with one suggestion per answer, and if someone has already answered with your favorite, please vote that answer up. Hopefully it will be easy to see what is most popular.

Thanks!

brian d foy
  • 129,424
  • 31
  • 207
  • 592
pkaeding
  • 36,513
  • 30
  • 103
  • 141

6 Answers6

37

Just for the record, here's a snippet that uses XML::LibXML.

#!/usr/bin/env perl

#
# Create a simple XML document
#

use strict;
use warnings;
use XML::LibXML;

my $doc = XML::LibXML::Document->new('1.0', 'utf-8');

my $root = $doc->createElement('my-root-element');
$root->setAttribute('some-attr'=> 'some-value');

my %elements = (
    color => 'blue',
    metal => 'steel',
);

for my $name (keys %elements) {
    my $tag = $doc->createElement($name);
    my $value = $elements{$name};
    $tag->appendTextNode($value);
    $root->appendChild($tag);
}

$doc->setDocumentElement($root);
print $doc->toString();

and this outputs:

<?xml version="1.0" encoding="utf-8"?>
<my-root-element some-attr="some-value">
    <color>blue</color>
    <metal>steel</metal>
</my-root-element>
toolic
  • 57,801
  • 17
  • 75
  • 117
Cosimo
  • 2,846
  • 1
  • 24
  • 26
22

XML::Writer is still maintained (at least, as of February of this year), and it's indeed one of the favorite Perl XML writers out there.

As for describing the syntax, one is better to look at the module's documentation (the link is already in the question). To wit:

use XML::Writer;

my $writer = new XML::Writer();  # will write to stdout
$writer->startTag("greeting", 
                  "class" => "simple");
$writer->characters("Hello, world!");
$writer->endTag("greeting");
$writer->end();

# produces <greeting class='simple'>Hello world!</greeting>
chbrown
  • 11,865
  • 2
  • 52
  • 60
Yanick
  • 1,250
  • 8
  • 9
  • Thanks, I did look at the documentation for XML::Writer. I was hoping for a somewhat cleaner syntax, so I will wait a bit to see if anyone else chimes in with any suggestions, but if this seems to be the favorite, than I will go with it. – pkaeding Sep 30 '08 at 20:47
  • 2
    Syntax "maybe" long in the tooth but XML::Writer as served me well for over 8 years now. Its the one I reach for first when needing to create XML from Perl. For something more "sexier" then check out XML::Class (however note this hasn't been updated since 2005). – draegtun Oct 01 '08 at 12:06
  • 3
    Re. "cleaner syntax", the "greeting" tag in the example above could have been written: $writer->dataElement("greeting", "Hello, world!", "class"=>"simple"). I don't see how much simpler this could be. – R. Hill Jul 18 '10 at 12:59
  • XML::Writer is maintained - that's what I use too. Unfortunately, it could still be better. As far as I know it doesn't perform self-enclosing tags. Plus, there's no way to add attributes after the tag has been written (it's not really object based). – vol7ron Sep 08 '10 at 00:56
9

If you want to take a data structure in Perl and turn it into XML, XML::Simple will do the job nicely.

At its simplest:

my $hashref = { foo => 'bar', baz => [ 1, 2, 3 ] };
use XML::Simple;
my $xml = XML::Simple::XMLout($hashref);

As its name suggests, its basic usage is simple; however it does offer a lot of features if you need them.

Naturally, it can also parse XML easily.

EDIT: I wrote this back in Oct 2008, 14 years ago this year. Things have changed since then. XML::Simple's own documentation carries a clear warning:

The use of this module in new code is strongly discouraged. Other modules are available which provide more straightforward and consistent interfaces. In particular, XML::LibXML is highly recommended and you can refer to for a tutorial introduction. XML::Twig is another excellent alternative.

These days, I'd strongly recommend checking those out rather than using XML::Simple in new code.

David Precious
  • 6,544
  • 1
  • 24
  • 31
  • 1
    XML::Simple is pretty awful at parsing XML, but it's downright *horrible* at generating it. So so so many things are out of your control. Even in your tiny example, it's unclear what output you will get. (Meaning it can vary from run to run.) [Why is XML::Simple Discouraged?](https://stackoverflow.com/q/33267765/589924) – ikegami Apr 27 '22 at 03:38
8

I don't do much XML, but XML::Smart looks like it might do what you want. Take a look at the section Creating XML Data in the doc and it looks very simple and easy to use.

Paraphrasing the doc:

use XML::Smart;

## Create a null XML object:
my $XML = XML::Smart->new() ;

## Add a server to the list:
$XML->{server} = {
    os => 'Linux' ,
    type => 'mandrake' ,
    version => 8.9 ,
    address => [ '192.168.3.201', '192.168.3.202' ] ,
} ;

$XML->save('newfile.xml') ;

Which would put this in newfile.xml:

<server os="Linux" type="mandrake" version="8.9">
  <address>192.168.3.201</address>
  <address>192.168.3.202</address>
</server>

Cool. I'm going to have to play with this :)

Matt Siegman
  • 155
  • 6
2

XML::Smart looks nice, but I don't remember it being available when I was using XML::Simple many years ago. Nice interface, and works well for reading and writing XML.

Bill Turner
  • 3,695
  • 1
  • 20
  • 26
  • It's actually truly horrible at generating XML. [Why is XML::Simple Discouraged?](https://stackoverflow.com/q/33267765/589924) – ikegami Apr 27 '22 at 03:39
1

I like XML::TreeBuilder because it fits the way I think. Historically, I've used it more for parsing than emitting.

A few weeks after this question was posted, I had occasion to generate some XML from Perl. I surveyed the other modules listed here, assuming one of them would work better than XML::TreeBuilder, but TreeBuilder was still the best choice for what I wanted.

One drawback was there is no way to represent processing instructions and declarations in an XML::TreeBuilder object.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
skiphoppy
  • 97,646
  • 72
  • 174
  • 218
  • I agree with you Brian. In my case, I decided to give XML::LibXML a try also for writing because the piece of code I was changing was already using XML::LibXML for other tasks. – Cosimo Jun 01 '10 at 20:25
  • 3
    Brian's just the editor who added the link; I'm the one who made the comment. :) – skiphoppy Jun 03 '10 at 20:43