|
-
Jun 27th, 2010, 06:17 PM
#1
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.
-
Jun 27th, 2010, 10:40 PM
#2
Thread Starter
Fanatic Member
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_parser, XML_OPTION_CASE_FOLDING, true);
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>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|