我有来自套接字的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&amp; XML ::简单但没有工作,他们只解析第一行...... 任何人都可以帮我从@cmdout数组中提取数据???
答案 0 :(得分:1)
在解析之前,您需要加入您的@cmdout
数组:
use XML::Simple;
my $cmd = join("", @cmdout);
my $ref = XMLin($cmd);