?
Solved

Perl parsing of XML

Posted on 2013-05-21
4
Medium Priority
?
437 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

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

Author Comment

by:nachtmsk
ID: 39185971
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 2000 total points
ID: 39185986
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
 
LVL 1

Author Comment

by:nachtmsk
ID: 39186033
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans

762 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