Page 1 of 1

XML Parsing: What is wrong with my code?

Posted: Fri May 28, 2010 5:21 pm
by dbol
My browser is showing absolutely nothing... a blank page.

What's wrong with my code?

Code: Select all

<?php 

$xml_file = "http://www.meltwaternews.com/magenta/xml/html/37/10/147839.html.XML"; 

$xml_title_key = "*DOCUMENTS*DOCUMENT*TITLE";
$xml_url_key = "*DOCUMENTS*DOCUMENT*URL";
$xml_igress_key = "*DOCUMENTS*DOCUMENT*INGRESS";
$xml_date_key = "*DOCUMENTS*DOCUMENT*CREATEDATE5";

$story_array = array(); 

$counter = 0; 
class xml_document{ 
    var $title, $url, $ingress, $date; 
} 

function startTag($parser, $data){ 
    global $current_tag; 
    $current_tag .= "*$data"; 
} 

function endTag($parser, $data){ 
    global $current_tag; 
    $tag_key = strrpos($current_tag, '*'); 
    $current_tag = substr($current_tag, 0, $tag_key); 
} 

function contents($parser, $data){ 
    global $current_tag, $xml_title_key, $xml_url_key, $xml_ingress_key, $xml_date_key, $counter, $document_array; 
    switch($current_tag){ 
        case $xml_title_key: 
            $document_array[$counter] = new xml_document(); 
            $document_array[$counter]->title = $data; 
            break; 
        case $xml_url_key: 
            $document_array[$counter]->url = $data; 
            $counter++; 
            break; 
        case $xml_ingress_key: 
            $document_array[$counter]->ingress = $data; 
            $counter++; 
            break; 
        case $xml_date_key: 
            $document_array[$counter]->date = $data; 
            $counter++; 
            break; 			
    } 
} 

$xml_parser = xml_parser_create(); 

xml_set_element_handler($xml_parser, "startTag", "endTag"); 

xml_set_character_data_handler($xml_parser, "contents"); 

$fp = fopen($xml_file, "r") or die("Could not open file"); 

$data = fread($fp, 80000) or die("Could not read file"); 

if(!(xml_parse($xml_parser, $data, feof($fp)))){ 
    die("Error on line " . xml_get_current_line_number($xml_parser)); 
} 

xml_parser_free($xml_parser); 

fclose($fp);

?> 

<html> 
<head> 
<title>Project: Parse XML 2</title> 
</head> 
<body bgcolor="#FFFFFF"> 
<?php 
for($x=0;$x<count($document_array);$x++){ 
    echo "\t<h2>" . $document_array[$x]->title . "</h2>\n"; 
    echo "\t\t\n"; 
    echo "\t<i>" . $document_array[$x]->url . "</i>\n"; 
	echo "\t\t\n"; 
    echo "\t<i>" . $document_array[$x]->ingress . "</i>\n";
	echo "\t\t\n";
	echo "\t<i>" . $document_array[$x]->date . "</i>\n";
} 
?> 

</body> 
</html>
The XML file can be found here: http://www.meltwaternews.com/magenta/xm ... 9.html.XML
It is also listed inside the code above.

Re: XML Parsing: What is wrong with my code?

Posted: Fri May 28, 2010 6:47 pm
by requinix
Open up your php.ini and edit

Code: Select all

error_reporting = E_ALL
display_errors = on
Then restart your web server and check the page again.

Re: XML Parsing: What is wrong with my code?

Posted: Fri May 28, 2010 7:09 pm
by phdatabase
At first glance, I don't see where contents() is called to create a $document_array. Second, it looks like the classic file reading loop but there is no looping (while) statement.

Re: XML Parsing: What is wrong with my code?

Posted: Fri May 28, 2010 7:59 pm
by dbol
I got that issue sorted out.

Thanks gentlemen.

Re: XML Parsing: What is wrong with my code?

Posted: Sat May 29, 2010 3:34 am
by internet-solution
For our benefits, do you want to share the issue and solution?

Re: XML Parsing: What is wrong with my code?

Posted: Sun May 30, 2010 10:45 am
by dbol
Sure. The problem was that I was incrementing the counter after every case (switch). I copied and pasted the code too quick without actually reading it. Lesson #1.