Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

Assembling XML in Perl

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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...