用于DOM的Perl XML解析器

时间:2017-10-10 13:40:56

标签: xml perl dom

我正在尝试解析Performance Statistics(3gpp 32.435)。结构并不像我希望的那么简单,见例子。我正在寻找的结果是使用

输出文本
    endTime:measType:measValue

我尝试使用perl脚本来提取值,但正如您所看到的,对于SGSN-MME_Session_SM_G,有两种类型和两种值,因此我的脚本的结果是

     SM.SuccActSecondPdpContext.G
     SM.UnsuccActPdpContextCC38.G

        1
        473764

如果有人可以给我一个提示,如何解析这个XML,或者如何从父母的两个孩子那里加入类型和值。我要感激,谢谢..

使用的脚本:

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

use XML::LibXML;

my $filename = 'file.xml';

my $dom = XML::LibXML->load_xml(location => $filename);

foreach my $measvalue ($dom->documentElement) {
        say $measvalue->to_literal();
    }

XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="MeasDataCollection.xsl"?>
<measCollecFile xmlns="http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec">
   <fileHeader fileFormatVersion="32.435 V9.0" vendorName="New Vendor">
      <fileSender />
      <measCollec beginTime="2017-10-09T20:17:00+03:00" />
   </fileHeader>
   <measData>
      <managedElement localDn="TestNode" />
      <measInfo measInfoId="SGSN-MME_Mobility_U">
         <job jobId="customPdcJob" />
         <granPeriod duration="PT60S" endTime="2017-10-09T20:18:00+03:00" />
         <repPeriod duration="PT60S" />
         <measType p="1">succGprsAttachUmts</measType>
         <measValue measObjLdn="ManagedElement=TestNode,SgsnFunction=1">
            <r p="1">132737759</r>
         </measValue>
      </measInfo>
      <measInfo measInfoId="SGSN-MME_Session_DiscardedMessages_G">
         <job jobId="customPdcJob" />
         <granPeriod duration="PT60S" endTime="2017-10-09T20:18:00+03:00" />
         <repPeriod duration="PT60S" />
         <measType p="1">discardedSessionMgmnt</measType>
         <measValue measObjLdn="ManagedElement=TestNode,SgsnFunction=1">
            <r p="1">12368</r>
         </measValue>
      </measInfo>
      <measInfo measInfoId="SGSN-MME_Session_SM_G">
         <job jobId="customPdcJob" />
         <granPeriod duration="PT60S" endTime="2017-10-09T20:18:00+03:00" />
         <repPeriod duration="PT60S" />
         <measType p="1">SM.SuccActSecondPdpContext.G</measType>
         <measType p="2">SM.UnsuccActPdpContextCC38.G</measType>
         <measValue measObjLdn="ManagedElement=TestNode,SgsnFunction=1">
            <r p="1">1</r>
            <r p="2">473764</r>
         </measValue>
      </measInfo>
      <measInfo measInfoId="SGSN-MME_Session_SM_U">
         <job jobId="customPdcJob" />
         <granPeriod duration="PT60S" endTime="2017-10-09T20:18:00+03:00" />
         <repPeriod duration="PT60S" />
         <measType p="1">SM.UnsuccActPdpContextCC38.U</measType>
         <measValue measObjLdn="ManagedElement=TestNode,SgsnFunction=1">
            <r p="1">737355</r>
         </measValue>
      </measInfo>
      <measInfo measInfoId="SGSN-MME_SystemResource_PIU">
         <job jobId="customPdcJob" />
         <granPeriod duration="PT60S" endTime="2017-10-09T20:18:00+03:00" />
         <repPeriod duration="PT60S" />
         <measType p="1">SYS.gsnApCpuUsage</measType>
         <measValue measObjLdn="ManagedElement=TestNode,SgsnMme=1,PIU=1.1">
            <r p="1">6</r>
         </measValue>
         <measValue measObjLdn="ManagedElement=TestNode,SgsnMme=1,PIU=1.3">
            <r p="1">5</r>
         </measValue>
         <measValue measObjLdn="ManagedElement=TestNode,SgsnMme=1,PIU=1.5">
            <r p="1">5</r>
         </measValue>
         <measValue measObjLdn="ManagedElement=TestNode,SgsnMme=1,PIU=1.7">
            <r p="1">5</r>
         </measValue>
      </measInfo>
   </measData>
   <fileFooter>
      <measCollec endTime="2017-10-09T20:18:00+03:00" />
   </fileFooter>
</measCollecFile>

1 个答案:

答案 0 :(得分:1)

use strict;
use warnings qw( all );
use feature qw( say );

use XML::LibXML               qw( );
use XML::LibXML::XPathContext qw( );

my $doc = XML::LibXML->load_xml( location => $filename );

my $xpc = XML::LibXML::XPathContext->new();
$xpc->registerNs( m => 'http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec' );

for my $measInfoNode ($xpc->findnodes('/m:measCollecFile/m:measData/m:measInfo', $doc)) {
   my $endTime = $xpc->findvalue('m:granPeriod/@endTime', $measInfoNode);
   my $measType = $xpc->findvalue('m:measType', $measInfoNode);
   for my $measValueNode ($xpc->findnodes('m:measValue', $measInfoNode)) {
      my $measValue = $xpc->findvalue('m:r', $measValueNode);
      say join ":", $endTime, $measType, $measValue;
   }
}