【问题标题】:XML Parsing With Perl using XML::Twig使用 XML::Twig 使用 Perl 进行 XML 解析
【发布时间】:2011-11-19 23:42:59
【问题描述】:

我需要从 XML 文件中获取一些值...该文件位于多个站点中。例如:http://1.1.1.1/apache2-default/status.xmlhttp://1.1.1.2/apache2-default/status.xml 等。XML 文件的格式为:

<?xml version="1.0"?>
<gateway>
<version>Kannel bearerbox version `1.4.3' </version>
<status>running, uptime 0d 14h 19m 49s</status>
<wdp>
<received><total>0</total><queued>0</queued></received>
<sent><total>0</total><queued>0</queued></sent>
</wdp>
<sms>
<received><total>50569</total><queued>0</queued></received>
<sent><total>38</total><queued>0</queued></sent>
<storesize>-1</storesize>
<inbound>1.53,1.52,0.98</inbound>
<outbound>0.00,0.00,0.00</outbound>
</sms>
<smscs><count>5</count>
<smsc>
<name>SMPP:1.2.3.4:3802/3802:hng123:HNDSMPP</name>
<id>hng123</id>
<status>online 51589s</status>
<received>0</received>
<sent>0</sent>
<failed>0</failed>
<queued>0</queued>
</smsc>
<smsc>
<name>SMPP:1.2.3.4:3757/3757:vashn:HNDSMPP</name>
<id>vas1</id>
<status>online 51589s</status>
<received>40872</received>
<sent>32</sent>
<failed>0</failed>
<queued>0</queued>
</smsc>
</smscs>
</gateway>

这是我创建的代码...

 #!/usr/bin/perl  -w
use strict;
use warnings;

use XML::Twig;
use Data::Dumper;
use LWP::Simple;

my $download_dir= "/root/lab_xml_perl/v2/Script_perl/bajados";
my @ips_server = ("192.168.246.129", "192.168.246.130");
for my $servers (@ips_server) {
my $bajo = getstore("http://$servers/apache2-default/status.xml",   "$download_dir/status.xml");

my $file = "$download_dir/status.xml";
my $twig = XML::Twig->new();
$twig->parsefile($file);

my $root = $twig->root->first_child('smscs');

for my $connection ($root->children){
my $nombre_server = $connection->first_child_text('name');
my $id_server = $connection->first_child_text('id');
my $status_server = $connection->first_child_text('status');
my @status_gral = split(/\s+/, ${status_server});
my $recibidos_msg_server = $connection->first_child_text('received');
my $enviados_msg_server = $connection->first_child_text('sent');
my $encolados_msg_server = $connection->first_child_text('queued');
my $fallidos_msg_server = $connection->first_child_text('failed');
print "miId.String.id = Tabla_$id_server\n";
print "stringProp.String = $nombre_server\n";
print "idProp = $id_server\n";
print "status_idObs.StringObservation.obs = $status_gral[0]\n";
print "recvTotal.Metric = $recibidos_msg_server\n";
print "sentTotal.Metric = $enviados_msg_server\n";
print "queuedTotal.Metric = $encolados_msg_server\n";
print "faildTotal.Metric = $fallidos_msg_server\n";

}
}

但是,当运行脚本时,我在开始时得到了一些空字段...和警告...每个服务器的每个 xml 文件...。 这是输出...为什么会这样?...我不需要它们...只需要与 XML 相关的字段..

miId.String.id = Tabla_
stringProp.String = 
idProp = 
Use of uninitialized value $status_gral[0] in concatenation (.) or string at ./v25.pl line 32.
status_idObs.StringObservation.obs = 
recvTotal.Metric = 
sentTotal.Metric = 
queuedTotal.Metric = 
faildTotal.Metric = 
miId.String.id = Tabla_hng123
stringProp.String = SMPP:1.2.3.4:3802/3802:hng123:HNDSMPP
idProp = hng123
status_idObs.StringObservation.obs = online
recvTotal.Metric = 0
sentTotal.Metric = 66173
queuedTotal.Metric = 0
faildTotal.Metric = 0 
miId.String.id = Tabla_vas1
stringProp.String = SMPP:1.2.3.4:3757/3757:vashn:HNDSMPP
idProp = vas1
status_idObs.StringObservation.obs = online
recvTotal.Metric = 0
sentTotal.Metric = 45492
queuedTotal.Metric = 0
faildTotal.Metric = 0
Use of uninitialized value $status_gral[0] in concatenation (.) or string at ./v25.pl line 32.
status_idObs.StringObservation.obs = 
recvTotal.Metric = 
sentTotal.Metric = 
queuedTotal.Metric = 
faildTotal.Metric = 
miId.String.id = Tabla_hng123
stringProp.String = SMPP:1.2.3.4:3802/3802:hng123:HNDSMPP
idProp = hng123
status_idObs.StringObservation.obs = online
recvTotal.Metric = 0
sentTotal.Metric = 0
queuedTotal.Metric = 0
faildTotal.Metric = 0
miId.String.id = Tabla_vas1
stringProp.String = SMPP:1.2.3.4:3757/3757:vashn:HNDSMPP
idProp = vas1
status_idObs.StringObservation.obs = online 
recvTotal.Metric = 40872
sentTotal.Metric = 32
queuedTotal.Metric = 0
faildTotal.Metric = 0

非常感谢您的帮助。

迈克尔。

【问题讨论】:

    标签: perl xml-twig


    【解决方案1】:

    未初始化值的原因是因为smscs 中的第一个孩子实际上是count

    <smscs>
        <count>5</count>
        <smsc>
            <name>SMPP:1.2.3.4:3802/3802:hng123:HNDSMPP</name>
            <id>hng123</id>
            <status>online 51589s</status>
            <received>0</received>
            <sent>0</sent>
            <failed>0</failed>
            <queued>0</queued>
        </smsc>
        <smsc>
            <name>SMPP:1.2.3.4:3757/3757:vashn:HNDSMPP</name>
            <id>vas1</id>
            <status>online 51589s</status>
            <received>40872</received>
            <sent>32</sent>
            <failed>0</failed>
            <queued>0</queued>
        </smsc>
    </smscs>
    

    因此,您只需获取smsc 的所有子代,而不是获取smscs 的所有子代:

    foreach my $connection ($root->children('smsc')) {
        ...
    }
    

    【讨论】:

    • 根据我的经验,$elt-&gt;children()(或first_child,或任何导航方法)几乎总是一个错误。您应该通过使用条件来限制导航,除非您有非常非常好的理由不这样做(并且您在下面的代码中测试您访问的元素)。
    • @mirod 您建议使用children 导航的替代方法是什么?
    • @stevenl 您使用的替代方案:$root-&gt;children('smsc') ;--) 使用标签名称或其他特征指定您想要的孩子类型,通常您没问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    相关资源
    最近更新 更多