Link to home
Create AccountLog in
Avatar of nachtmsk
nachtmskFlag for United States of America

asked on

Perl parsing of XML

Hi,
I'm having a hard time getting some XML parsing to work.
Here's the story.
I am posting an XML file to a server for authentication.
The server  I post to responds back with some XML. In that XML is a token that I need to parse out.
The data that I am being sent back is part XML and part HTTP header. I just can't seem to get the XML isolated to parse it. I'm thinking it has something to do with line breaks but I'm not sure. I have tried using the XML::Simple as well as regular expressions but nothing is working.   Attached is the file I am getting returned from the server I am posting to. This is the file I need to parse the XML out of, just a few fields really, <Token> being the most important and <Status> being the other. Thanks for any help!

I have attached the XML file I get back from the server I am posting to. I changed a few things, but just values in the Tags, nothing else.
Here is the code I am using. It's commented out a lot because I have tried various methods.

I guess the big issue I seem to be having  when using XML::Simple is:
*The XML returned has Non-XML in it, so XML::Simple is choking. I tried using RegEx to parse out just the XML from the returned file but nothing is working. I'm pretty decent with RegEx so I'm just not getting what is happening. Thanks!


use CGI;
#use DBI;
use LWP::UserAgent;
use HTTP::Request::Common;
use XML::Simple;
#use Data::Dumper;

my($query)= new CGI;

$u="test";
$p="test";


print $query->header();

my $userAgent = LWP::UserAgent->new(agent => 'perl post');

$message="<?xml version=\"1.0\" encoding=\"iso-8859-1\"?> <NotificationRequest Version=\"1.0\"> <NotificationHeader> <Type>2</Type>  Snip....";

my $response = $userAgent->request(POST 'http://www.posttosite.com/web_service.php',
Content_Type => 'text/xml',
Content => $message);

print $response->error_as_HTML unless $response->is_success;

$res =$response->as_string;

#open(FILE,">>mobile_sso.xml");
#print FILE $res;
c#lose(FILE);


$res=~/\<Token\>(.*)/;

print $1;


#@a = split /\n/, $res;

#print $a[0];
mobile-sso.xml
Avatar of ozo
ozo
Flag of United States of America image

Did you want  
$res=~/<Token>(.*)<\/Token>/s;
or
$res=~/(<NotificationService.*)/s;
?
Avatar of nachtmsk

ASKER

Ozo. I wanted the first one, the value between the <TOKEN> Tags.
Thanks
What is the  s switch at the end?
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thanks ozo. That will come in handy.
I posted to a perl newsgroup as well and got another good answer.
My problem was in this line:
$res =$response->as_string;
it should be
$res =$response->content

Using the 'as_string' method doesn't strip out the HTTP header. That header is what was causing me all the trouble. once it was gone, I could use XML:Simple and Data::Dumper to get my data. The RegEx solution you gave me will be useful in other things. Thanks