Connecting Tech Pros Worldwide Help | Site Map

Parsing an XML (same TAG name)

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 10:47 AM
m|sf|t
Guest
 
Posts: n/a
Default Parsing an XML (same TAG name)

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, 10:47 AM
m|sf|t
Guest
 
Posts: n/a
Default 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)


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.