Jump to content

Help with XML


Balmung-San

Recommended Posts

[code]
<?
global $c, $tag_name, $parent_name;
global $blurbs, $titles, $bodies, $dates;
global $images, $videos;
global $labels, $types, $urls;
$c=-1;
function startTag($parser, $name, $attrs) {
  global $c, $tag_name, $parent_name;
global $labels, $types, $urls;
global $images, $videos;
  global $dates, $Ms, $Ds, $Ys;
  $tag_name = $name;
  switch($name) {
case "ITEM":
$c++;
$blurbs[$c]='';
$bodies[$c]='';
$titles[$c]='';
$labels[$c]='';
$types[$c]='';
$urls[$c]='';
$videos[$c]='';
$images[$c]='';
break;
case "DATE":
$date = $attrs["M"]."/".$attrs["D"]."/".$attrs["Y"];
$Ms[$c] = $attrs["M"];
$Ds[$c] = $attrs["D"];
$Ys[$c] = $attrs["Y"];
$dates[$c] = $date;
break;
case "LINK":
$parent_name='link';
break;
case "MEDIA":
$parent_name='media';
break;
}
}

function tagData($parser, $tagData) {
  global $c, $tag_name, $parent_name;
  global $blurbs, $titles, $bodies;
  global $images, $videos;
global $labels, $types, $urls;
  switch($tag_name) {
  case "BLURB":
  $blurbs[$c]=$tagData;
  break;
  case "TITLE":
$titles[$c]=$tagData;
  break;
  case "BODY":
$bodies[$c] .= $tagData;
  break;
  case "LABEL":
  if($parent_name=='link')
$labels[$c]=$tagData;
  break;
  case "TYPE":
  if($parent_name=='link')
$types[$c]=$tagData;
break;
  case "URL":
  if($parent_name=='link')
$urls[$c]=$tagData;
break;
  case "VIDEO":
  if($parent_name=='media')
$videos[$c]=$tagData;
  break;
  case "IMAGE":
if($parent_name=='media')
$images[$c]=$tagData;
  break;
  }
  $tag_name = "";
}

function endTag($parser, $name) {
$parent_name="";

}

$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE,false);
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "tagData");
$file = '../path_to_xml_file/home.xml';
$data = xml_parse($xml_parser,file_get_contents($file));
xml_parser_free($xml_parser);

if(!$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)));
}

?>

[/code]
Link to comment
https://forums.phpfreaks.com/topic/16018-help-with-xml/#findComment-65988
Share on other sites

looking at the code above, you have 3 functions

startTag - gets called when the parser reaches a start tag

tagData - will get all the data

endTag - gets called when the parser reaches an end tag

so pretty much it stores the xml into a global array that I can access in other pages
Link to comment
https://forums.phpfreaks.com/topic/16018-help-with-xml/#findComment-65992
Share on other sites

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.