Connecting Tech Pros Worldwide Help | Site Map

Parsing an XML (same TAG name)

  #1  
Old July 17th, 2005, 11:47 AM
m|sf|t
Guest
 
Posts: n/a
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;", "%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;%", "&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 =)


  #2  
Old July 17th, 2005, 11:47 AM
m|sf|t
Guest
 
Posts: n/a

re: Parsing an XML (same TAG name)


Yeah fuckers =) I figured it out...I'll post back soon w/my solution (it's
pretty ghetto, but it works)


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
String parsing question... timslavin@gmail.com answers 5 July 27th, 2006 07:35 AM
javascript and XML help rbann11@hotmail.com answers 2 July 25th, 2006 04:15 PM
xsl:element must have a value for name - help ina answers 4 June 13th, 2006 12:05 PM
Parsing out embedded XML Rob Archibald answers 2 July 20th, 2005 09:22 PM