Results 1 to 4 of 4

Thread: Parse zipped (gz) xml file with php issues

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Parse zipped (gz) xml file with php issues

    I have a file that is ftp'd to my root and it's in .gz format. I need to uncompress and read/parse xml string. I run and it bombs on fopen. Is there a different way of doing it?

    zdemo.php
    PHP Code:
    <?php
    $feed_max 
    2;
    $topheader "News";
    $feed_path dirname(__FILE__) . "\Catalog_1.xml.gz";
    include(
    'rss.php'); 
    ?>
    rss.php
    PHP Code:
    <div class="rss">
    <h5><?php echo $topheader?></h5>
    <?php

    //set a max value of 3 listings if variable is empty
    if (empty($feed_max)){
        
    $feed_max=3;
    }

    $ip=$_SERVER['REMOTE_ADDR'];
    $file $feed_path;

    $map_array = array(
       
    "BOLD"    => "B",
       
    "EMPHASIS" => "I",
       
    "LITERAL"  => "TT"
    );

    $current "";
    $sLink "";
    $sSiteHost "";
    $sDescription "";
    $sTitle "";
    $GLOBALS['iStart'] = 0;
    $GLOBALS['iRowCount'] = 0;
    $GLOBALS['ifeed_max'] = $feed_max;

    function 
    startElement_mioutdoor($parser$name$attrs)
    {
        
    $name strtoupper($name);
       global 
    $current;
       
    $current $name;
       if (
    $name == "ITEM") { echo "<p>"; }
       if (
    $name == "LISTING") { echo ""; } 
       if (
    $name == "TITLE") { echo ""; }
       if (
    $name == "DESCRIPTION") { echo ""; }
       if (
    $name == "PUBDATE") { echo ""$GLOBALS['iStart'] = 1; }
    }

    function 
    endElement_mioutdoor($parser$name)
    {
       
    $name strtoupper($name);
       if (
    $name == "DESCRIPTION") { echo ""; }
       if (
    $name == "LISTING") { echo ""; }
       if (
    $name == "TITLE") { echo ""; }
        
       if (
    $GLOBALS['iStart'] == 1) {
           if (
    $GLOBALS['iRowCount']  < $GLOBALS['ifeed_max']) {
               if (
    $name == "ITEM") {echo "<p><a href=\"" $GLOBALS['sLink'] . "\" target=\"_blank\">" $GLOBALS['sTitle'] . "</a><br />" $GLOBALS['sDescription'] . "</p>"$GLOBALS['sTitle'] = ""$GLOBALS['sDescription'] = ""$GLOBALS['iRowCount'] ++;
               }
            }
           }
        if (
    $name == "ITEM") { echo "</p>"; }
    }

    function 
    characterData_mioutdoor($parser$data)
    {
       global 
    $current;
       if (!(
    ord($data) == 10)){
               if (
    $GLOBALS['iStart'] == 1) {
               if (
    $current == "TITLE") { $GLOBALS['sTitle'] .= $data; }
               if (
    $current == "DESCRIPTION") { $GLOBALS['sDescription'] .= $data; } 
               if (
    $current == "LINK") { $GLOBALS['sLink'] = $data; }
               if (
    $current == "URL") { $GLOBALS['sSiteHost'] = $data; }
           }
       }
    }

    $xml_parser xml_parser_create();
    // use case-folding so we are sure to find the tag in $map_array
    xml_parser_set_option($xml_parserXML_OPTION_CASE_FOLDINGtrue);
    xml_set_element_handler($xml_parser"startElement_mioutdoor""endElement_mioutdoor");
    xml_set_character_data_handler($xml_parser"characterData_mioutdoor");

    //NEW LINE TO TAKE .gz FILE AND UNPACK
    $file gzfile_get_contents($file);

    //THIS IS WHERE IT'S ERRORING
    if (!($fp fopen($file"r"))) {
       die(
    "could not open XML input");
    }


    while (
    $data fread($fp4096)) {
       
    $data convToUtf8($data);
       if (!
    xml_parse($xml_parser$datafeof($fp))) {
           die(
    sprintf("XML error: %s at line %d",
                       
    xml_error_string(xml_get_error_code($xml_parser)),
                       
    xml_get_current_line_number($xml_parser)));
       }
    }


    xml_parser_free($xml_parser);


    function 
    gzfile_get_contents($filename$use_include_path 0)
    {
        
    //File does not exist
        
    if( !@file_exists($filename) )
        {    return 
    false;    }
       
        
    //Read and imploding the array to produce a one line string
       
    $data gzfile($filename$use_include_path);
       
    $data implode($data);  
       
       return 
    $data;
    }



    ?>
    </div>
    Result of running zdemo.php
    HTML Code:
    Warning: fopen(<?xml version="1.0" e........ in D:\www\domain.us\httpdocs\rss.php
    could not open XML input
    Catalog_1.xml.gz (partial)
    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE product_catalog SYSTEM "http://www.cj.com/downloads/tech/dtd/product_catalog_1_1.dtd">
    
    <catalog>
    <product>
    <programname>U.S. Cavalry Affiliate Program</programname>
    
    <programurl>http://www.uscav.com</programurl>
    <catalogname>Product Catalog 1</catalogname>
    <lastupdated>02/04/2009</lastupdated>
    <name>FOX&amp;reg; Response Vest</name>
    <keywords>Response Vests AH1004 AE1004 AA1004 AH AE AA 1004 aj aj1004 Law Enforcement Protective Gear Black HKC1004 JBC1004 JBC 1004 JFC1004 JFC 1004 HKC1004 HKC 1004 JFC1004 JFC 1004 JLC1004 JLC 1004 lbv LBV LBV kdm1004 kdm 1004 kdl1004 kdl 1004 KDF1004 KDF 1004 LGC LGC1004 LKG1004 LKC1004 fox military gear</keywords>
    <description>4 mag pockets, 2 utility pockets, and snap loops for hook-up to a pistol belt.</description>
    <sku>1004</sku>
    <manufacturer>FOX OUTDOOR PRODUCTS</manufacturer>
    
    <manufacturerid>1</manufacturerid>
    <currency>USD</currency>
    <price>30.99</price>
    <buyurl>http://www.dpbolvw.net/click-3594573-10273726?url=http%3A%2F%2Fwww.uscav.com%2Fproductinfo.aspx%3FTabID%3D548%26productid%3D5287%26cm_mmc%3DCJ-_-Affiliate-_-ProductCatalog-_-1004</buyurl>
    <impressionurl>http://www.ftjcfx.com/image-3594573-10273726</impressionurl>
    <imageurl>http://www.uscav.com/prodinfo/images/1004.jpg</imageurl>
    <instock>YES</instock>
    </product>
    </catalog>

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Parse zipped (gz) xml file with php issues

    I have a hunch that you didn't write any of this code.

    the function you have there, gzfile_get_contents(), returns the contents of the gzipped file. it doesn't return a filename that you can open. you only need to parse it from then on.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Re: Parse zipped (gz) xml file with php issues

    You got a hunch ey? Well, yes I did find most of the functions online is true but using them to work for me, yes I did make modifications to them. And yes your pretty good, gzfile_get_contents does what you say and I believe if you look at the code I entered it also said this, maybe instead of blasting me you should provide a positive response that helps everyone.

    Yes, I figured it out. Here is my changed rss file that has mostly copied functions but slight modifications to work for me.

    rss.php
    PHP Code:
    <div class="rss">
    <h5><?php echo $topheader?></h5>
    <?php
    //set a max value of 3 listings if variable is empty
    if (empty($feed_max)){
        
    $feed_max=3;
    }


    $ip=$_SERVER['REMOTE_ADDR'];

    $file $feed_path;

    $map_array = array(
       
    "BOLD"    => "B",
       
    "EMPHASIS" => "I",
       
    "LITERAL"  => "TT"
    );

    $current "";
    $sName "";
    $sDescription "";
    $sPrice "";
    $sBuyUrl "";
    $sImageUrl "";
    $sInStock "";
    $GLOBALS['iStart'] = 0;
    $GLOBALS['iRowCount'] = 0;
    $GLOBALS['ifeed_max'] = $feed_max;

    //XML FILE FORMAT
    /*
    <catalog>
    <product>
    <programname>U.S. ..</programname>
    <programurl>http://www...</programurl>
    <catalogname>Product ..</catalogname>
    <lastupdated>02/04/2009</lastupdated>
    <name>FOX..</name>
    <keywords>Response ..</keywords>
    <description>4 mag ...</description>
    <sku>1004</sku>
    <manufacturer>FOX ..</manufacturer>
    <manufacturerid>1</manufacturerid>
    <currency>USD</currency>
    <price>30.99</price>
    <buyurl>http://www....</buyurl>
    <impressionurl>http://www.ftjcfx....</impressionurl>
    <imageurl>http://www.uscav....</imageurl>
    <instock>YES</instock>
    </product>
    </catalog>

    */


    function startElement_mioutdoor($parser$name$attrs)
    {
        
    //IS FIRST THING CALLED FOR EACH ROW AND $name IS NODE NAME
        
    $name strtoupper($name);
       global 
    $current;
       
    $current $name;
       if (
    $name == "PRODUCT") { echo ""$GLOBALS['iStart'] = 1; }    
    }

    function 
    endElement_mioutdoor($parser$name)
    {
        
    //IS CALLED AFTER characterData_mioutdoor AND $name IS NODE NAME
       
    $name strtoupper($name);
        
       if (
    $GLOBALS['iStart'] == 1) {   
           if (
    $GLOBALS['iRowCount']  < $GLOBALS['ifeed_max']) {
               if (
    $name == "INSTOCK") {echo "<p><a href=\"" $GLOBALS['sBuyurl'] . "\" target=\"_blank\">" $GLOBALS['sName'] . "</a><br />" $GLOBALS['sDescription'] . "<br />Price: $" $GLOBALS['sPrice'] . "<br />InStock: <strong>" $GLOBALS['sInstock']  . "</strong><br /><img src='" $GLOBALS['sImageurl'] . "'></p>"
               
               
    //CLEAR VARIABLES       
               
    $GLOBALS['sName'] = ""
               
    $GLOBALS['sDescription'] = ""
               
    $GLOBALS['sPrice'] = "";
               
    $GLOBALS['sBuyurl'] = "";
               
    $GLOBALS['sImageurl'] = "";
               
    $GLOBALS['sInstock'] = "";
               
               
    //Increase row count
               
    $GLOBALS['iRowCount'] ++;
               }
            }
           }
    }

    function 
    characterData_mioutdoor($parser$data)
    {
        
    //IS CALLED AFTER characterData_mioutdoor AND $name IS NODE NAME
        
       
    global $current;
       
       
               if (
    $GLOBALS['iStart'] == 1) {
                
    //echo "current: " . $current . " data: " . $data . "=" . ord($data) . "<br />";
               
    if ($current == "NAME") { $GLOBALS['sName'] .= $data; }
               if (
    $current == "DESCRIPTION") { $GLOBALS['sDescription'] .= $data; } 
               if (
    $current == "PRICE") { $GLOBALS['sPrice'] .= $data; }
               if (
    $current == "BUYURL") { $GLOBALS['sBuyurl'] .= $data; }
               if (
    $current == "IMAGEURL") { $GLOBALS['sImageurl'] .= $data; }
               if (
    $current == "INSTOCK") { $GLOBALS['sInstock'] .= $data; }
           }
    }

    $xml_parser xml_parser_create();
    // use case-folding so we are sure to find the tag in $map_array
    xml_parser_set_option($xml_parserXML_OPTION_CASE_FOLDINGtrue);
    xml_set_element_handler($xml_parser"startElement_mioutdoor""endElement_mioutdoor");
    xml_set_character_data_handler($xml_parser"characterData_mioutdoor");
    $data gzfile_get_contents($file);
    if (!
    xml_parse($xml_parser$data)) {
           die(
    sprintf("XML error: %s at line %d",
                       
    xml_error_string(xml_get_error_code($xml_parser)),
                       
    xml_get_current_line_number($xml_parser)));
       }


    xml_parser_free($xml_parser);




    function 
    gzfile_get_contents($filename$use_include_path 0)
    {
        
    //File does not exist
        
    if( !@file_exists($filename) )
        {    return 
    false;    }
       
        
    //Read and imploding the array to produce a one line string
       
    $data gzfile($filename$use_include_path);
       
    $data implode($data);
       
       
    $dstName dirname(__FILE__) . "\test.xml";
       
       return 
    $data;
    }

    ?>
    </div>

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Parse zipped (gz) xml file with php issues

    maybe instead of blasting me you should provide a positive response that helps everyone
    I was not "blasting" you, and my response was not as negative as you're making it out to be. I thought it was quite neutral; my statement about my hunch was just that -- a statement.

    I believe if you look at the code I entered it also said this
    Almost none of the code you posted is commented, so no -- it does not say this anywhere.

    Basically, if you're trying to learn something you should learn what the functions you're using are doing, so that you actually understand how they work and why they work. I told you how the function you had worked, which I hope was beneficial to you. When you see a function and you don't know how it works, search for it on PHP.net.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width