在Perl中组装XML [英] Assembling XML in Perl
问题描述
我需要对NetApp文件管理器进行API调用. 我知道我需要发送什么原始XML:
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>
我已经开始组装成纯文本",我一直在尝试使用XML::Twig
做得更好".
Having started assembling as 'plain text', I've been trying to 'do it better' with XML::Twig
.
但是我很难插入前两行,因为它们不是XML树的一部分".
But I'm having difficulty with inserting the first two lines, as they're not 'part' of the XML tree.
我已经挖出XML::Twig::Elt
并发现我可能需要set_pi
才能获得第一行,但是...好吧,想要的输出有些困难.
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;
并分别:
my $header = XML::Twig::Elt -> new () -> set_pi('xml', 'version="1.0" encoding="utf-8"');
$header -> print;
对于DOCTYPE,我有:
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"');
它掩盖了内容.我在这里想念什么?有没有插入该初始xml
行的更好的方法?
It clobbers the content. What am I missing here? Is there a better way of inserting that initial xml
line?
但是那并不完全有效,因为我需要该行-在我的doctype之前.
But that doesn't quite work, because I need that line - before my doctype.
例如:
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;
产生:
<!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...
推荐答案
我在挖掘过程中发现了它-set_pi
是一个红色的鲱鱼.我真正想要的是:
I've found it with a bit of digging - set_pi
was a red herring. What I really want is:
XML::Twig
方法:set_xml_version
,set_encoding
和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;
注意-要通过LWP发送,您需要的是$request -> content ( $twig -> sprint )
;
Note - to send via LWP, what you'll need is $request -> content ( $twig -> sprint )
;
这篇关于在Perl中组装XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!