|
Posted by patrice.fiset@gmail.com on 07/01/07 22:13
Hello,
I have a simple script, basically it looks through the XML file and
when the parser is at the tag 'title', on the function charElement I
display OK, but for some reason, the number 1 is getting display. I
really don't get it here.
Thanks for help and here's the code.
<?
include("includes/connexion.php");
// Global variables for function use.
$GLOBALS['item'] = false;
$GLOBALS['title'] = false;
// function: startElement
// Deals with the starting element
function startElement( $parser, $tagName, $attrs ) {
// By setting global variable of tag name
// I can determine which tag I am currently
// parsing.
switch( $tagName ) {
case 'ITEM':
$GLOBALS['item'] = true;
break;
case 'TITLE':
$GLOBALS['title'] = true;
break;
}
}
function endElement( $parser, $tagName ) {
//$repl = array('é' => 'é','è' => 'è','ç' =>
'ç','ê' => 'ê','ë' => 'ë','î' => 'î','ï' =>
'ï','ô' => 'ô','ö' => 'ö','ù' => 'ù','ö' =>
'ö');
// By noticing the closing tag,
// I can print out the data that I want.
if ($GLOBALS['item'] === true)
{
switch( $tagName ) {
case 'TITLE':
//echo "<tr><td class='boldDev'
colspan='2'>".str_replace(array_keys($repl), array_values($repl),
$GLOBALS['titletext'])."</a></td></tr>";
print $GLOBALS['title']."<br>";
$GLOBALS['title'] = false;
$GLOBALS['title'] = "";
break;
}
}
}
// function: charElement
// Deals with the character elements (text)
function charElement( $parser, $text ) {
// Verify the tag that text belongs to.
// I set the global tag name to true
// when I am in that tag.
if ($GLOBALS['item'] === true)
{
if ($GLOBALS['title'] === true)
print "OK";
//print $text;//print htmlspecialchars( trim($text) );//
$GLOBALS['title'] .= htmlspecialchars( trim($text) );
}
}
// Create an xml parser
$xmlParser = xml_parser_create();
// Open connection to RSS XML file for parsing.
$fp = fopen( "http://www.mckaulick.com/feeds/mondernier.xml", "r" )
or die( "Cannot read RSS data file." );
// Set up element handler
xml_set_element_handler( $xmlParser, "startElement", "endElement" );
// Set up character handler
xml_set_character_data_handler( $xmlParser, "charElement" );
// Parse XML data from RSS file.
while( $data = fread( $fp, 4096 ) ) {
xml_parse( $xmlParser, $data, feof( $fp ) );
}
// Close file open handler
fclose( $fp );
// Free xml parser from memory
xml_parser_free( $xmlParser );
?>
[Back to original message]
|