Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

You need to join your @cmdout array before parsing:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...