在 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.
到目前为止我有:
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?
我发现:如何读取和更改标签和<?xml version="1.0"?>在 xml 树枝中?
但这并不完全有效,因为我需要那一行 - 在我的文档类型之前.
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 ->内容 ( $twig -> sprint )
;
Note - to send via LWP, what you'll need is $request -> content ( $twig -> sprint )
;
这篇关于在 Perl 中组装 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!