-1

我有一个来自套接字的 XML 数据

{

  my $xmlstr = "<ClearQuest login='XXXX' password='XXXX' db='XXX' repo='XXX'><defect id='dts0100' action='view' wait='yes'> </defect> </ClearQuest> ";                # read in str (or entire fh)
  my $exit   = 0;                             # default exit val = okay
  my @cmdout = ();                            # init command output
  my $server = 'ABCSERVER';            # cq server
  my $eof    = '</ClearQuest>';               # end of data tag
  my $port   = 5555;                          # port address of server
  my $proto  = getprotobyname( 'tcp' );       # get protocol num for tcp
  my $iaddr  = inet_aton( $server );          # convert hostname to bin ip
  my $paddr  = sockaddr_in( $port, $iaddr );  # resolve socket address

                                              # create socket
  socket( SOCK, PF_INET, SOCK_STREAM, $proto ) or die( "socket: $!" );
                                              # connect to socket
  connect( SOCK, $paddr ); #or die( "$errhdr unable to connect to '$server'!\n" );
  autoflush SOCK 1;                           # don't buffer to socket
  print( SOCK "$xmlstr\n" );                  # send command through socket
  print "DONE";
  shutdown( SOCK, 1 );       # we're done writing if enabled

  while ( $_ = <SOCK> )                       # while data in socket
  {
      if ( $_ =~ /status='error'/o )          # error detected
      {
            print "error!!!";
          $exit = 1;                          # set bad exit val
      }
      push( @cmdout, $_ );        # save command output
      last if ( $_ =~ /$eof/ );           # stop read if end of data
  }
  close( SOCK );           

}

我想解析 @cmdout 数组以提取返回的数据。

这是返回数据的示例

`

<ClearQuest db='XXX' login='XXXX' cqtan='1319' client='XXX.com' ip=''>
  <defect id='dts0100' action='view' status='ok'>
     <component>RA_Checks</component>
     <description>Please, discuss before resolution.</description>
     <headline>[CSV] Got a warning </headline>
     <id_short>799</id_short>
     <owner>ABC</owner>
     <planned_release.name>2013</planned_release.name>
   </defect>
 </ClearQuest>

`

我尝试使用 XML::Parser & XML::Simple 但没有用,他们只解析第一行......谁能帮我从@cmdout 数组中提取数据???

4

1 回答 1

1

您需要在解析之前加入您的数组:@cmdout

use XML::Simple;
my $cmd = join("", @cmdout);
my $ref = XMLin($cmd);
于 2013-07-14T12:06:05.713 回答