Jump to content

Archived

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

Balmung-San

Help with XML

Recommended Posts

Okay, so I've looked through the documentation and a few tutorials, and I am completely lost on how PHP parses XML. About all I'm wishing to do with it right now is taking an XML file and printing the information from one tag. Any help(or code) would be appreciated.

Share this post


Link to post
Share on other sites
I believe that xml is client side (like HTML) so it is not parsed by PHP (server side).  it can be generated by PHP, but not parsed by it.

Share this post


Link to post
Share on other sites
I just finished building an xml parser, give me about 10 minutes and I'll post you some code to look at so you can see how it works.

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.