我需要对NetApp文件管理器进行API调用。 我知道我需要发送什么原始XML:
<? 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>
开始组装“纯文本”后,我一直在努力做得更好&#39;与XML::Twig
。
但是我在插入前两行时遇到了困难,因为他们并没有“排除”部分&#39;部分&#39; XML树的。
我挖出XML::Twig::Elt
并想出我可能需要set_pi
来获得第一行,但是......好吧,我对所需的输出有些困难。
到目前为止,我已经:
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,我得到了:
my $twig = XML::Twig -> new ();
$twig -> set_root($content);
$twig -> set_doctype('netapp SYSTEM "file:/etc/netapp_filer.dtd"');
$twig -> print;
但我能弄清楚的是如何合并&#39;标题进入它。如果我做一个简单的事情:
$twig -> root -> set_pi('xml', 'version="1.0" encoding="utf-8"');
它破坏了内容。我在这里错过了什么?是否有更好的方法来插入初始xml
行?
我发现:how to read and change <!Doctype> tag and <?xml version="1.0"?> in xml twig?
但这并不是很有效,因为我需要那条线 - 在我的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;
产地:
<!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>
哪个很接近,但不完全......
答案 0 :(得分:2)
我发现它有点挖掘 - set_pi
是一只红鲱鱼。我真正想要的是:
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 )
;