XML Parsing: What is wrong with my code?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dbol
Forum Newbie
Posts: 3
Joined: Fri May 28, 2010 5:18 pm

XML Parsing: What is wrong with my code?

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
User avatar
phdatabase
Forum Commoner
Posts: 83
Joined: Fri May 28, 2010 10:02 am
Location: Fort Myers, FL

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

Post 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.
dbol
Forum Newbie
Posts: 3
Joined: Fri May 28, 2010 5:18 pm

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

Post by dbol »

I got that issue sorted out.

Thanks gentlemen.
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

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

Post by internet-solution »

For our benefits, do you want to share the issue and solution?
dbol
Forum Newbie
Posts: 3
Joined: Fri May 28, 2010 5:18 pm

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

Post 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.
Post Reply