All,
I am stuck processing an XML file. The problem I am having, is I have the
same TAG name - Url - at the same depth, so the value from the last Url read
in is what get saved. I have tried every option I can think of to put each
URL tag into its own variable, without any luck.
I removed portions of the code that does not affect the outcome (for a
smaller post).
Here's a snippet of the XML, and when the code (below) runs, no matter what,
the value of $items[$itemcount]['url'] is always
/TiVoVideoDetails?id=465887.
- <CustomIcon>
<Url>urn:tivo:image:save-until-i-delete-recording</Url>
<ContentType>image/*</ContentType>
<AcceptsParams>No</AcceptsParams>
</CustomIcon>
- <TiVoVideoDetails>
<Url>/TiVoVideoDetails?id=465887</Url>
<ContentType>text/xml</ContentType>
<AcceptsParams>No</AcceptsParams>
</TiVoVideoDetails>
PHP code:
<?php
class Tivo_XML {
var $dvr;
function parseTiVoXML()
{
global $items;
$this->parser = xml_parser_create();
xml_set_object($this->parser, &$this);
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($this->parser, "_tivostart_element",
"_tivoend_element");
xml_set_character_data_handler($this->parser, "_tivodata");
$file = $this->dvr . "_nowplaying.xml";
$tivoxmlstr = file_get_contents ($file);
$tivoxmlstr = str_replace("&", "%amp;%", $tivoxmlstr);
xml_parse($this->parser, $tivoxmlstr , true) or
die (sprintf("XML Error: %s at line %d",
xml_error_string(xml_get_error_code($this->parser)),
xml_get_current_line_number($this->parser)));
xml_parser_free($this->parser);
return $items;
}
function _tivostart_element($parser, $name, $attribs)
{
global $depth, $currenttag, $items, $itemcount;
$depth++;
if ($name == "Url" && $depth == 5) {
$currenttag = $name;
}
}
function _tivoend_element($parser, $name)
{
global $depth, $items;
$depth--;
}
function _tivodata($parser, $data)
{
$data = str_replace("%amp;%", "&", $data);
global $currenttag, $items, $itemcount;
switch ($currenttag) {
case "Url":
$items[$itemcount]['url'] = $data;
$currenttag = "";
break;
default:
break;
}
}
}
?>
Does this make sense to anyone, cause I confused as hell =)