The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to send a Head Http request from command line darkling235 UNIX for Dummies Questions & Answers 3 04-03-2008 11:48 PM
HTTP request noufal UNIX for Dummies Questions & Answers 3 01-31-2008 01:55 AM
http request forward piltrafa UNIX for Dummies Questions & Answers 4 12-06-2007 09:40 AM
Http request in Linux malaysoul Shell Programming and Scripting 7 01-26-2007 08:25 AM
unix script http request arksal UNIX for Dummies Questions & Answers 1 08-24-2006 07:15 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-06-2005
Registered User
 

Join Date: Feb 2005
Location: Columbus OH
Posts: 133
HTTP Query Request & Parsing returned XML output

I have a PERL script from which I need to make a HTTP request to Web Servlet (Essentially a URL with variables and values like &Variable1=AAAAAA&Variable2=BBBBBBBBB&Variable3=CCCCCCC). The Web servlet returns an XML result which needs to be parsed for the contents of the result within the program.

Here is what I need to know
1) How to make this http request from PERL (What modules would I require for this request) and
2) How do I read the results from the XML results that the query returns?

Any help and or suggestions provided is greatly appreciated. Thank you.
Reply With Quote
Forum Sponsor
  #2  
Old 05-06-2005
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,477
(1) You can always construct the query string from the parameters manually. It's just a matter of 'key=value' pairs with '&' in between (as you have mentioned). If you have the list of key-value pairs ready, this should be easy but you need to watch out that non-alphanumeric characters should be escaped by the %XY notation, where XY is the hex. representation of the character's ASCII value.

Or, you can use the modules URI and URI::QueryParam which I think is safer.

Code:
use URI;
use URI::QueryParam;
$u = new URI("http://someserver.com/control/servlet");
%aryParams = (
    'test1' => 'value1',
    'test2' => '$abc#def',
);
foreach (keys %aryParams) {
	$u->query_param($_, $aryParams{$_});
}
print $u->as_string() . "\n";
Output:
Code:
http://someserver.com/control/servlet?test1=value1&test2=%24abc%23def
Note that the module will do the necessary escaping for you, automatically.

Finally, use LWP::Simple to fire the HTTP request.

(2) For reading XML files, I have previously tried using XML::Simple. It builds a data structure corresponding to the XML file. Suitable for small XML files that are not so complicated but it is quite convenient.

A more complex alternative is XML::Parser, with a more-or-less SAX-like API. I haven't used this before in practice. And there are many other choices of XML parsing modules available on CPAN. Go to search.cpan.org and have a look.

I have recently worked on a PHP project with lots of reading and processing from XML files. I decided to use XPath extensively and found that I required far less code with XPath than conventional "tree-walk" mechanisms. You can extract an arbitrary node anywhere in the document with some precise conditions specified just as SQL in database queries, which is handy for more complex analysis. In Perl, an equivalent module is XML::XPath.

You need to download most of these modules as they are not in the core distribution.
Reply With Quote
  #3  
Old 05-10-2005
Registered User
 

Join Date: Feb 2005
Location: Columbus OH
Posts: 133
Thank you for the guidance and update cbkihong. We are using AIX 5.2.0 build and the sys admin says that he has installed all optional modules for PERL.
In your post you indicate three packages. URI, LWP and XML. I think I have URI::QueryParam;
LWP::Simple;
XML::Simple;

As indicated I do not have XML::Parser and XML::XPath. Can you please suggest a link on cpan where I might find these or do I need to go to IBM to obtain the above. Also can any suggested reading material for XML::XPath and XML::Parser either in book form or web-sites? Thanks again.

Jerardfjay

Last edited by jerardfjay; 05-10-2005 at 04:23 AM.
Reply With Quote
  #4  
Old 05-10-2005
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
A search of http://search.cpan.org/ will give you what you need - I've just checked and they're there...

Cheers
ZB
Reply With Quote
  #5  
Old 05-10-2005
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,477
Honestly speaking, to my dismay I have found some XML modules for Perl are not that mature, still (the ones I mentioned are okay). Some only implement a part of the W3C standards (though it may be actually quite usable by most people). For many modules, the documentation are not well-written and some may even mislead you into antiquated material. So I tend to go Google for bits and pieces. But I think if you have an idea of the specs, you will find that the API for these XML technologies for most programming languages are highly familiar, so resources written for other languages may also be useful. Just try the examples you can find, bits and pieces, from the Web and you will soon find it easy to follow.

I think you will find this sample chapter excerpt useful:
http://www.webreference.com/programm...perlxml/chap3/

While I did my PHP project at work I read directly the W3C spec for XPath. But that is not any pleasurable reading after all. I also read the relevant chapters of the J2EE Tutorial for info (it's for Java, of course). For a simple introduction, you may go to w3schools.com.

For modules installation, modules on CPAN can be downloaded and installed with a simple (single!) command on the command line! A past poster here has written a short tutorial for that:

PERL & CPAN Intro for Newbies

Refer him/her here to take a look.
Reply With Quote
  #6  
Old 05-10-2005
Registered User
 

Join Date: Feb 2005
Location: Columbus OH
Posts: 133
Thank you all for the wonderful insights into the world of PERL. I am still learning new stuff everyday. This makes this site truly wonderful learning experience. Thanks again.

Jerardfjay
Reply With Quote
  #7  
Old 05-11-2005
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,477
Quote:
Originally Posted by jerardfjay
I am still learning new stuff everyday.
So are we. All. In

http://www.perl.com/pub/a/2000/06/27/perlbook.html

an active Perl community member wrote that

Quote:
You can't learn Perl in 24 hours, 21 days, 12 weeks, 9 months, or a year. I've been programming Perl for nearly five years and I'm still learning.
How true. Especially for a language like Perl which allows you to do virtually anything in an uncountable no. of ways. Probably only Larry Wall and a handful few elites can really claim they are done learning. Not ever for the rest of us here.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0