简体   繁体   English

Perl中的简单XML操作

[英]Simple XML Manipulations in perl

I know SO doesn't honor this type of questions, but I have invested a lot of time trying to choose the best XML manipulation library for perl and almost no expample I've found provides a clear understanding on how to insert values and print out the resulting xml to a string variable. 我知道SO不接受此类问题,但是我花了很多时间试图为perl选择最佳的XML操作库,而我发现几乎没有示例可以清楚地了解如何插入值和打印输出将生成的xml转换为字符串变量。

Here is my incoming XML without values: 这是我传入的没有值的XML:

my $GetLibByNameAndVersionEnvelope = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
                                            <s:Body>
                                                <GetLibByNameAndVersion xmlns="LibService">
                                                    <name></name>
                                                    <version></version>
                                                </GetGridLibByNameAndVersion>
                                            </s:Body>
                                        </s:Envelope>';

I need to populate the name and the version nodes with test values. 我需要用测试值填充名称和版本节点。 Please provide an example on how can I do this, so in a result, I have a string that containes a required XML. 请提供一个有关如何执行此操作的示例,因此,结果是,我有一个包含所需XML的字符串。

This is a SOAP request, right? 这是一个SOAP请求,对不对?

If it is just this one simple SOAP call that always looks the same, I'd suggest you use a template engine , put in placeholders and don't bother with XML modules at all. 如果只是一个简单的SOAP调用始终看起来相同,我建议您使用模板引擎 ,放在占位符中 ,根本不要理会XML模块。 That's going to be a lot cheaper. 那会便宜很多。

You can either use something like Text::Template and put the template somewhere in your filesystem (so you can add it to version control) or simply add it to your code in the DATA section or even as a simple string variable. 您可以使用诸如Text :: Template之类的东西并将模板放置在文件系统中的某个位置(以便可以将其添加到版本控制中),也可以将其简单地添加到DATA节中的代码中,甚至可以将其添加为简单的字符串变量。 You could of course just put it in as a string like this: 您当然可以将其作为这样的字符串放入:

my $ua = LWP::UserAgent->new;
my $res = $ua->post($url, <<"SOAP");
<s:Envelope>
  <s:Body>
    <GetLibByNameAndVersion xmlns="LibService">
      <name>$name</name>
      <version>$version</version>
    </GetGridLibByNameAndVersion>
  </s:Body>
</s:Envelope>
SOAP

That example is not very dynamic and you need to change the code if there are changes to the API. 该示例不是很动态,如果API发生更改,则需要更改代码。 But for a very simple request, this is the quickest way. 但是对于一个非常简单的请求,这是最快的方法。 I feel sometimes this (or a template) is the way to go if you only have one rather simple method in your API. 如果您的API中只有一个相当简单的方法,我有时会觉得这是一种方法(或模板)。

Update: Did you know that Perl has a whole set of modules specifically to deal with SOAP? 更新:您是否知道Perl有专门用于处理SOAP的整套模块? This article seems like a good place to start. 本文似乎是一个不错的起点。

XML::Twig has a list of tutorials on their website . XML::Twig其网站上有教程列表。 I recommend this tutorial , which is old but will definitely cover all of the things you are interested in. 我建议使用本教程该教程虽然很旧,但肯定会涵盖您感兴趣的所有内容。

Also, CPAN documentation almost always has good simple examples of how to use a module. 同样,CPAN文档几乎总是提供有关如何使用模块的简单示例。 XML::Twig 's documentation certainly does . XML::Twig的文档当然可以 So does XML::Simple . XML::Simple

Here is an article on using XML::Simple . 这是有关使用XML::Simple的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM