From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of eric
clark
        Sent: 14 September 2005 21:36
        To: [email protected]
        Subject: XML Parser question
        
        

        I have this program that uses XML::Parser.  The Program runs
fine up until the parse() function is called, and then something doesn't
seem to work right.  $hashref should be populated with data, but it is
empty.  I have checked and made sure that $xmldata contains the proper
xml data.  Does anybody have any ideas?

        # LOAD MODULES
        use XML::Parser;
        use HTTP::Request;
        use LWP::UserAgent;
        use Net::FTP;
        use strict;

        # DEBUG VARIABLES
        # set debug to 1 to enable debug mode, otherwise set to 0
        my $debug = 1;
        # enter file containing xml feed to use in debug mode
        my $debug_xmlfilename = "feed.xml";
        # ftp server to use in debug mode
        my $debug_username = "somebody";
        my $debug_password = "somewhere";
        my $debug_ftpserver = "somehow";

        
        # CUSTOM INFORMATION
        my $froogle_username = "";
        my $froogle_password = "";
        my $froogle_ftpserver = "";
        my $store_xmlfeed_url =
"http://store.yahoo.com/storename/objinfo.xml";;
        # set to "Windows" or "Linux"; needed to find temp directory
        my $operating_system = "Windows";

        # DECLARE VARIABLES
        my $xmlparser;
        my $temp_directory;
        my $froogle_data_filename;
        my $xmldata; # string containing yahoo store xml data
        my $user_agent;
        my @redirectable_methods = (); # no methods should be
redirectable
        my $response;
        my $fh_outfile; # file handle to the output file
        my $filedata;
        my @productarray;
        my %product;
        my $key;
        my $hashref;
        my $ftp;

        # DEFINE XML PARSER SUBROUTINES
        $xmlparser = new XML::Parser(Handlers => {
            Start => \&tag_start,
            End => \&tag_end,
            Char => \&handle_char });

        # INITIALIZE VARIABLES
        if($debug) { print "Yahoo Store Export to Froogle - Debug.\n"; }

        # figure out the temp directory path
        if($operating_system eq "Windows")
        {
            $temp_directory = $ENV{TEMP}."\\";
        }
        if($operating_system eq "Linux")
        {
            $temp_directory = "/var/tmp/";
        }

        # set the filename for the temporary froogle data file
        $froogle_data_filename =
$temp_directory.$froogle_username.".txt";

        if($debug)
        {
            print "Froogle data file name is: ";
            print $froogle_data_filename."\n";
        }

        # open the file for output
        open FH_OUTFILE, ">feedl.txt" or die ("Error opening file for
write: $!");

        # print column names
        print FH_OUTFILE 
        
"product_url\tname\tdescription\tprice\timage_url\tcategory\tcode\n";

        # RETRIEVE THE ENTIRE XML FEED INTO A STRING
        if (!$debug)
        {
            # create a user agent
            $user_agent = LWP::UserAgent->new();
            # disable redirection
            $user_agent->requests_redirectable([EMAIL PROTECTED]);
            # perform a get request
            $response = $user_agent->get($store_xmlfeed_url);
            # check the response
            die ("Error while getting ".$response->request->uri.
                "\nStatus-Line: ".$response->status_line."\nAborting")
              unless ($response->is_success);

            # copy the response data into a string
            $xmldata = $response->content;
        }
        else
        {
            # read xml data from a debug file
            open DEBUGXMLFILE, "<".$debug_xmlfilename;
            while(read(DEBUGXMLFILE, $filedata, 10000))
            {
                $xmldata .= $filedata;
            }
            
            close DEBUGXMLFILE;
        }

        # RUN THE XML PARSER (PARSING IS DONE IN THE SUBROUTINES)
        $xmlparser->parse($xmldata);

        ## Dies right around here ##
        print "$hashref\n";
        # WRITE THE PARSED DATA TO THE TEMP FILE
        for $hashref (@productarray)
        {
           print FH_OUTFILE $hashref->{product_url}."\t";
            print FH_OUTFILE $hashref->{name}."\t";
            print FH_OUTFILE $hashref->{description}."\t";
            print FH_OUTFILE $hashref->{price}."\t";
            print FH_OUTFILE $hashref->{image_url}."\t";
            print FH_OUTFILE $hashref->{category}."\t";
            print FH_OUTFILE $hashref->{code};
            print FH_OUTFILE "\n";
        }

        # debug print the parsed data
        if(0)
        {
            for $hashref (@productarray)
            {
                print "\n\n";
                for $key (keys %$hashref)
                {
                    print "'$key' => '$hashref->{$key}' \n";
                }
            }
        }

        # close the output file
        close (FH_OUTFILE);

        if($debug)
        {
            $froogle_ftpserver = $debug_ftpserver;
            $froogle_username = $debug_username;
            $froogle_password = $debug_password;
        }
        

It would help us to help you if you posted a small, self contained
script that demonstrated your problem. Something that we can cut and
paste and run. Also expressions like "doesn't seem to work right" don't
give us much of a clue as to what your problem is. For more help see
http://www.catb.org/~esr/faqs/smart-questions.html.

In the mean time, a few comments on the code you did post.

- "use strict" and "use watnings" should be at the start
- Avoid global variables. Prefer to declare variables in a small a scope
as possible
- Always check system calls like 'open' for success.
- There are easier ways to slurp a whole file, see File::Slurp
- To check your OS see special variable $^O (see 'perldoc perlvar')
- I couldn't seem to find your XML::Parser handler functions

HTH

-- 
Brian Raven
 


-----------------------------------------------------------------------
The information contained in this e-mail is confidential and solely 
for the intended addressee(s). Unauthorised reproduction, disclosure, 
modification, and/or distribution of this email may be unlawful. If you 
have received this email in error, please notify the sender immediately 
and delete it from your system. The views expressed in this message 
do not necessarily reflect those of LIFFE Holdings Plc or any of its subsidiary 
companies.
-----------------------------------------------------------------------


_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to