Hej
Jeg har kastet mig ud i at ville parse noget XML data, jeg har på
fornemmelsen at jeg måske griber det lidt forkert an, jeg sende en
SOAP envelope, og får et fint resultat tilbage, men hvordan får jeg
det parset korrekt? Jeg tror det er min måde at tage min respons og
parse den som går galt, jeg kan nemlig uden problem kopiere resultatet
ned i en fil, og så derefter åbne den og parse, men når jeg forsøger
at parse data i min variable ($response) direkte, så går det galt.
Nogle gode råd eller ideer ?
###Start script###
use strict;
use LWP::UserAgent;
use HTTP::Request::Common;
use XML::Parser;
my $userAgent = LWP::UserAgent->new(agent => 'perl post');
my $message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<soapenv:Envelope
xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">
<soapenv:Header />
<Body>
<ns:Login xmlns:ns=\"http://schemas.ericsson.com/cai3g1.1/\">
<ns:userId>xxxxxx</ns:userId>
<ns:pwd>xxxxxx</ns:pwd>
</ns:Login>
</Body>
</soapenv:Envelope>";
my $response = $userAgent->request(POST 'http://x.x.x.x',
Content_Type => 'text/xml',
Content => $message);
print $response->error_as_HTML unless $response->is_success;
print $response->as_string;
### PARSER ###
# initialize parser object and parse the string
my $parser = XML::Parser->new( ErrorContext => 2 );
eval { $parser->parse( $response ); };
# report any error that stopped parsing, or announce success
if( $@ ) {
$@ =~ s/at \/.*?$//s; # remove module line number
print STDERR "\nERROR \...@\n";
} else {
print STDERR " Data is well-formed\n";
}
###End of script ###
Resultatet af at køre ovenstående script er:
HTTP/1.1 200 OK
Connection: keep-alive
Server: gSOAP/2.2
Content-Length: 699
Content-Type: text/xml; charset=utf-8
Client-Date: Wed, 09 Jun 2010 00:55:05 GMT
Client-Peer: 10.9.2.107:8998
Client-Response-Num: 1
<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns
:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:ns="http://schemas.ericsson.com/cai3g1.0/2003/05/30/"
xmlns:ns2="http://schemas.
ericsson.com/cai3g1.0/2004/01/22/"
xmlns:ns3="http://schemas.ericsson.com/cai3g1.1/"><SOAP-ENV:Header></SOAP-ENV:Header><SOAP-ENV:Body><ns3:LoginResponse><ns3:sessionId>84b0bf8a84b
0bf8a000000001276044913734</ns3:sessionId><ns3:baseSequenceId>11898686317761921024</ns3:baseSequenceId></ns3:LoginResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
ERROR
not well-formed (invalid token) at line 1, column 14, byte 14:
HTTP::Response=HASH(0x1d5f0fc)
=============^
at C:/Perl/lib/XML/Parser.pm line 187