Assembling XML in Perl

I’ve found it with a bit of digging – set_pi was a red herring. What I really want is:

XML::Twig methods: set_xml_version, set_encoding, and set_doctype.

use strict;
use warnings;

use XML::Twig;

my $twig = XML::Twig->new( 'pretty_print' => 'indented' );
$twig->set_root(
    XML::Twig::Elt->new(
        'netapp',
        {   version => 1.7,
            vfiler  => "somevfiler",
            xmlns   => "http://www.netapp.com/filer/admin",
        },
    )
);
my $api_req = $twig->root->insert_new_elt('nfs-exportfs-list-rules');
   $api_req ->insert_new_elt( 'pathname', '/vol/path/to/somewhere' );
   $api_req -> insert_new_elt ('your mum' );
   # etc.

$twig->set_doctype('netapp SYSTEM "file:/etc/netapp_filer.dtd"');
$twig->set_xml_version("1.0");
$twig->set_encoding('utf-8');

$twig->print;

Note – to send via LWP, what you’ll need is $request -> content ( $twig -> sprint );

Leave a Comment