Solved

Perl parsing of XML

Posted on 2013-05-21
4
392 Views
Last Modified: 2013-05-21
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
0
Comment
Question by:nachtmsk
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Did you want  
$res=~/<Token>(.*)<\/Token>/s;
or
$res=~/(<NotificationService.*)/s;
?
0
 

Author Comment

by:nachtmsk
Comment Utility
Ozo. I wanted the first one, the value between the <TOKEN> Tags.
Thanks
What is the  s switch at the end?
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
perldoc perlre
PERLRE(1)             User Contributed Perl Documentation            PERLRE(1)

NAME
       perlre - Perl regular expressions

DESCRIPTION
       This page describes the syntax of regular expressions in Perl.
...
   Modifiers
       Matching operations can have various modifiers.  Modifiers that relate
       to the interpretation of the regular expression inside are listed
       below.  Modifiers that alter the way a regular expression is used by
       Perl are detailed in "Regexp Quote-Like Operators" in perlop and "Gory
       details of parsing quoted constructs" in perlop.
...
       s   Treat string as single line.  That is, change "." to match any
           character whatsoever, even a newline, which normally it would not
           match.
0
 

Author Comment

by:nachtmsk
Comment Utility
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
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now