Advertisement

07.06.2007 at 04:55AM PDT, ID: 22679066
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.0

How to open XML file, read contents and POST via HTTP

Asked by kloner in Perl Programming Language

Hi All

I have spent days trying to do a simple task, yet am having no luck at all!

I am looping through a directory, containing xml files. For each file, I am trying to open the file, read the contents, and post it to a URL.

My problem is, i'm not sure if I am reading the file correctly and posting the contents. Also how do I encode the xml contents?

The code is as follows:



sub sendXMLDocs
{
      my $userAgent = LWP::UserAgent->new(agent => 'Mozilla/5.0 (WinNT; I)');

      my $pattern = $outDir."*\.xml";
      foreach my $fileName (glob($pattern))
      {
            # Open the file for readonly
            open(XML_FILE_CONTENTS, $fileName);

            #post the file contents to infoMedia
            my $response = $userAgent->request(POST 'http://1.httpfeeds.XXXXXXXX.com/SKY/SSSSS/POST/',
                                                                     Content_Type => 'application/x-www-form-urlencoded',
                                                                     Content => XML_FILE_CONTENTS);

            #Close the file.
            close(XML_FILE_CONTENTS);


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


            if ($response->is_success) {

                  #Read the XMl response from Infomedia
                  #[
                  #<?xml version="1.0" standalone="yes"?>
                  #<result>
                  #        <event>01</event>
                  #        <status>EMPTY</status>
                  #        <text>The input was empty</text>
                  #</result>
                  #]

                  #Get the response from InfoMedia
                  my $xmlResponse = $response->content;

                  #Strip all carriage returns & line feeds
                  $xmlResponse =~ s/\r\n//g;


            $logFile->log ("xmlResponse    : $xmlResponse");

                  #Load response XML
                  my $objParser       = XML::DOM::Parser->new ();      
                  my $requestDoc = $objParser->parse ($xmlResponse);

                  #Grab code, description, details from xml response.
                  my $xmlResponseCode         = $requestDoc->find ('/result/event')->string_value ();
                  my $xmlResponseDesc    = $requestDoc->find ('/result/status')->string_value ();
                  my $xmlResponseDetails = $requestDoc->find ('/result/text')->string_value ();

                  print "xmlResponseCode    : ".$xmlResponseCode."\n";
                  print "xmlResponseDesc    : ".$xmlResponseDesc."\n";
                  print "xmlResponseDetails : ".$xmlResponseDetails."\n\n\n";

                  #Check the response
                  if ($xmlResponseCode == "01")
                  {
                        $logFile->log ("FILE NAME    : $fileName");
                        $logFile->log ("ERROR CODE   : 01");
                        $logFile->log ("ERROR DESC   : $xmlResponseDesc");
                        $logFile->log ("ERROR DETAILS: $xmlResponseDetails");
                        $logFile->log ("EXPLANATION  : A valid XML payload could not be found.\n\n\n");
                  }
                  elsif ($xmlResponseCode == "02")
                  {
                        $logFile->log ("FILE NAME    : $fileName");
                        $logFile->log ("ERROR CODE   : 02");
                        $logFile->log ("ERROR DESC   : $xmlResponseDesc");
                        $logFile->log ("ERROR DETAILS: $xmlResponseDetails");
                        $logFile->log ("EXPLANATION  : Includes a message giving more details on the nature of the error.      Invalid XML markup was submitted.\n\n\n");
                  }
                  elsif ($xmlResponseCode == "03")
                  {
                        $logFile->log ("FILE NAME    : $fileName");
                        $logFile->log ("ERROR CODE   : 03");
                        $logFile->log ("ERROR DESC   : $xmlResponseDesc");
                        $logFile->log ("ERROR DETAILS: $xmlResponseDetails");
                        $logFile->log ("EXPLANATION  : If this error is encountered please notify the administrator, informing them of the Result_Details.\n\n\n");
                  }
                  elsif ($xmlResponseCode == "00")
                  {
                        #Successful post. Delete the file.
                        $logFile->log ("FILE NAME    : $fileName\n");
                        $logFile->log ("EXPLANATION  : The feed was submitted successfully.\n");

                        #Try and delete the file.
                        $deleted = unlink($fileName);
                        if ($deleted)
                        {
                              $logFile->log ("FILE DELETED\n\n\n");
                        }
                        else
                        {
                              $logFile->log ("***FILE NOT DELETED***\n\n\n");
                        }
                  }
            } else {
                  #Error occured posting to InfoMedia
                  print $response->error_as_HTML;

                  $logFile->log ("FILE NAME: $fileName\n");
                  $logFile->log ("ERROR    : Could not post file. $response->error_as_HTML\n\n\n");
            }
      }
}      


Thanks for any help!Start Free Trial
 
Keywords: How to open XML file, read contents …
 
Loading Advertisement...
 
[+][-]07.06.2007 at 05:01AM PDT, ID: 19431015

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.09.2007 at 08:20AM PDT, ID: 19445881

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Perl Programming Language
Sign Up Now!
Solution Provided By: Adam314
Participating Experts: 1
Solution Grade: B
 
 
 
Loading Advertisement...
20081119-EE-VQP-49