Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

XML Parser from command line in Perl

I have an XML data coming from a socket

{

  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'!
" );
  autoflush SOCK 1;                           # don't buffer to socket
  print( SOCK "$xmlstr
" );                  # 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 );           

}

I want to Parse the @cmdout array to extract the returned data.

Here is a sample of the returned data

`

<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>

`

I tried use XML::Parser & XML::Simple but didnt work,, they only parse the first line... Can anyone help me extract data from @cmdout array???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to join your @cmdout array before parsing:

use XML::Simple;
my $cmd = join("", @cmdout);
my $ref = XMLin($cmd);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...