Assembling XML in Perl

2019-01-09 19:00发布

问题:

I'm needing to make API calls to a NetApp filer. I know what raw XML I need to send:

<? xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE netapp SYSTEM "file:/etc/netapp_filer.dtd">
<netapp version="1.7" vfiler="somevfiler" xmlns="http://www.netapp.com/filer/admin">
    <nfs-exportfs-list-rules>
        <pathname>/vol/path/to/somewhere</pathname>
    </nfs-exportfs-list-rules>
</netapp>

Having started assembling as 'plain text', I've been trying to 'do it better' with XML::Twig.

But I'm having difficulty with inserting the first two lines, as they're not 'part' of the XML tree.

I've dug out XML::Twig::Elt and figured out I probably need to set_pi to get the first line, but ... well, am having some difficulty with the desired output.

Thus far I've got:

use strict;
use warnings;

use XML::Twig;

my $content = XML::Twig::Elt->new(
    'netapp',
    {   version => 1.7,
        vfiler  => "somevfiler",
        xmlns   => "http://www.netapp.com/filer/admin",
    },
);
$content->insert_new_elt('nfs-exportfs-list-rules')
    ->insert_new_elt( 'pathname', '/vol/path/to/somewhere' );

$content->set_pretty_print('indented');
$content->print;

and separately:

my $header = XML::Twig::Elt -> new () -> set_pi('xml', 'version="1.0" encoding="utf-8"');
$header -> print;

For the DOCTYPE I've got:

my $twig = XML::Twig -> new ();
$twig -> set_root($content);
$twig -> set_doctype('netapp SYSTEM "file:/etc/netapp_filer.dtd"');

$twig -> print;

But what I can't figure out is how to 'merge' the header into it. If I do a simple:

$twig -> root -> set_pi('xml', 'version="1.0" encoding="utf-8"');

It clobbers the content. What am I missing here? Is there a better way of inserting that initial xml line?

I've found: how to read and change <!Doctype> tag and <?xml version="1.0"?> in xml twig?

But that doesn't quite work, because I need that line - before my doctype.

E.g.:

my $twig = XML::Twig -> new ( 'pretty_print' => 'indented' );
$twig -> set_root($content);
$header -> move ( before => $twig -> root );
$twig -> set_doctype('netapp SYSTEM "file:/etc/netapp_filer.dtd"');

$twig -> print;

Produces:

 <!DOCTYPE netapp SYSTEM "file:/etc/netapp_filer.dtd">

  <?xml version="1.0" encoding="utf-8"?><netapp version="1.7" vfiler="somevfiler" xmlns="http://www.netapp.com/filer/admin">
  <nfs-exportfs-list-rules>
    <pathname>/vol/path/to/somewhere</pathname>
  </nfs-exportfs-list-rules>
</netapp>

Which is close, but not quite there...

回答1:

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 );