Jump to content

[SOLVED] I am going insane - Reading xml problem


jgd

Recommended Posts

Maybe I am being ridiculously stupid here but for the life of me I cannot figure out what is wrong with my code here, at the end of this parser I am trying to echo a certain xml element depending on a string, so that I can pass it on from a form, any chance any of you guys can spot a problem?

 

 

 

Code:

<?php

if( ! ($fp = fopen( "./sponsor1.xml" , "r" )) )

  die("Couldn't open xml file!");

$person_counter = 0;

$person_data = array();

$xml_current_tag_state = '';

function startElementHandler( $parser, $element_name, $element_attribs )

{

  global $person_counter;

  global $person_data;

  global $xml_current_tag_state;

  if( $element_name == "STORY" )

  {

    $person_data[$person_counter]["author"] = $element_attribs["AUTHOR"];

  }

  else

  {

    $xml_current_tag_state = $element_name;

  }

}

function endElementHandler( $parser, $element_name )

{

  global $person_counter;

  global $person_data;

  global $xml_current_tag_state;

  $xml_current_tag_state = '';

  if( $element_name == "STORY" )

  {

    $person_counter++;

  }

}

function characterDataHandler( $parser , $data )

{

  global $person_counter;

  global $person_data;

  global $xml_current_tag_state;

  if( $xml_current_tag_state == '' )

    return;

if( $xml_current_tag_state == "STORYTYPE" ) {

    $person_data[$person_counter]["storytype"] = $data;

  }

 

  if( $xml_current_tag_state == "HEADLINE" ) {

    $person_data[$person_counter]["headline"] = $data;

  }

  if( $xml_current_tag_state == "ABSTRACT" ) {

    $person_data[$person_counter]["abstract"] = $data;

  }

  if( $xml_current_tag_state == "BODYTEXT" ) {

    $person_data[$person_counter]["bodytext"] = $data;

  }

}

if( !($xml_parser = xml_parser_create()) )

  die("Couldn't create XML parser!");

 

xml_set_element_handler($xml_parser, "startElementHandler", "endElementHandler");

xml_set_character_data_handler($xml_parser, "characterDataHandler");

while( $data = fread($fp, 4096) )

{

  if( !xml_parse($xml_parser, $data, feof($fp)) )

  {

    break; // get out of while loop if we're done with the file

  }

}

xml_parser_free($xml_parser);

 

 

echo $person_data[0]["headline"]

 

 

?>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

 

<html>

<head>

<title>Untitled</title>

</head>

 

<body>

<?php

// This is the line that defines what type of article is shown

//i.e. the page needs to be fed the article string

//Here it's just set as news (For Now)

$article = "news";

 

 

 

for

(

$i=0 ;

$i < $person_counter ;

++$i

)

 

//

 

 

{

$thisName = $person_data[$i]["storytype"];

 

if ($thisName == $article)

{

echo "<b>we have match at position number: ". $i . "</b><br>";

$thisPos = $i;

}

}

 

 

 

echo $person_data[$thisPos]["headline"]

 

?>

</body>

</html>

 

Any help Would be really appreciated, Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.