简体   繁体   English

使用Perl解析XML文件

[英]Parse an XML file using Perl

I am trying to extract the value of two particular attributes from an XML file, whose structure is below; 我试图从XML文件中提取两个特定属性的值,其结构如下;

<environment>
    <applications>
       <application1>
          <app-config>
             <server host="boxA" port="1234"/>
           </app-config>
       </applicaitons> 
</environment>

I want to be able to read the value of the attribute "host" and "port". 我希望能够读取属性“host”和“port”的值。

I've tried with the foillowing piece of code but this doesn't work for me. 我已经尝试了一段代码,但这对我不起作用。

#!/usr/local/bin/perl -w

use XML::XPath;

my $file = "configuration.xml";
my $xp = XML::XPath->new(filename => $file);

my $hname = $xp->find('/environment/applications/application1/app-config/server/@host');
my $pnumber = $xp->find('/environment/applications/application1/app-config/server/@port');


print $hname;

But this does not return any output whatsoever when I run this command. 但是,当我运行此命令时,这不会返回任何输出。

Thanks in advance 提前致谢

Your XML is invalid! 你的XML无效! Fix it and it works fine. 修复它,它工作正常。

$ perl test.pl
boxA

Always, always start your perl scripts with; 总是,总是用你的perl脚本开始;

use strict;

And while debugging, also do this; 在调试的同时,也可以这样做;

use warnings;

That will show you that your XML is malformed to start with. 这将向您显示您的XML开始时格式不正确。

Fix your XML and it will work! 修复你的XML,它会工作!

</applicaitons> should be spelled as </applications> Replace that in your XML document. </applicaitons>应拼写为</applications>替换XML文档中的那个。 The source is fine. 来源很好。

Use XML:Simple. 使用XML:简单。 Its, well, simple. 它很简单。

Trying the following code: 尝试以下代码:

use strict;
use warnings;
use XML::Simple;
my $xml = XMLin( <<XML );
<environment>
    <applications>
       <application1>
          <app-config>
             <server host="boxA" port="1234"/>
           </app-config>
       </applicaitons> 
</environment>
XML
print $xml->{"applications"}{"app-config"}{"server"}{"host"} . "\n";
print $xml->{"applications"}{"app-config"}{"server"}{"port"} . "\n";

on your XML snippet, you'll get back an error such as: 在您的XML代码段上,您将收到如下错误:

mismatched tag at line 7, column 9, byte 159 at C:/Perl64/lib/XML/Parser.pm line 187

since its telling me there's a mismatched tag, I start examining the XML until I work out the malformed issues so I work on the XML errors until I come up with: 因为它告诉我标签不匹配,所以我开始检查XML,直到我找出格式错误的问题,因此我处理XML错误,直到我提出:

use strict;
use warnings;
use XML::Simple;
my $xml = XMLin( <<XML );
<environment>
    <applications>
          <app-config>
             <server host="boxA" port="1234"/>
           </app-config>
       </applications> 
</environment>
XML
print $xml->{"applications"}{"app-config"}{"server"}{"host"} . "\n";
print $xml->{"applications"}{"app-config"}{"server"}{"port"} . "\n";

And now the program yields the expected: 现在该程序产生预期:

boxA
1234

As you can see, it helped me quickly discover the source of the error and with no extra configuration XML::Simple made a very natural mapping to the perl hashes that we all love so well :-) ... simple. 正如你所看到的,它帮助我快速发现错误的来源,并且没有额外的配置XML :: Simple对我们都非常喜欢的perl哈希进行了非常自然的映射:-) ...简单。

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

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