Parsing an XML (same TAG name)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • m|sf|t

    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
    /TiVoVideoDetail s?id=465887.

    - <CustomIcon>
    <Url>urn:tivo:i mage:save-until-i-delete-recording</Url>
    <ContentType>im age/*</ContentType>
    <AcceptsParams> No</AcceptsParams>
    </CustomIcon>
    - <TiVoVideoDetai ls>
    <Url>/TiVoVideoDetail s?id=465887</Url>
    <ContentType>te xt/xml</ContentType>
    <AcceptsParams> No</AcceptsParams>
    </TiVoVideoDetail s>

    PHP code:
    <?php
    class Tivo_XML {
    var $dvr;
    function parseTiVoXML()
    {
    global $items;
    $this->parser = xml_parser_crea te();
    xml_set_object( $this->parser, &$this);
    xml_parser_set_ option($this->parser, XML_OPTION_CASE _FOLDING, false);
    xml_set_element _handler($this->parser, "_tivostart_ele ment",
    "_tivoend_eleme nt");
    xml_set_charact er_data_handler ($this->parser, "_tivodata" );
    $file = $this->dvr . "_nowplaying.xm l";
    $tivoxmlstr = file_get_conten ts ($file);
    $tivoxmlstr = str_replace("&a mp;", "%amp;%", $tivoxmlstr);
    xml_parse($this->parser, $tivoxmlstr , true) or
    die (sprintf("XML Error: %s at line %d",
    xml_error_strin g(xml_get_error _code($this->parser)),
    xml_get_current _line_number($t his->parser)));
    xml_parser_free ($this->parser);
    return $items;
    }
    function _tivostart_elem ent($parser, $name, $attribs)
    {
    global $depth, $currenttag, $items, $itemcount;
    $depth++;
    if ($name == "Url" && $depth == 5) {
    $currenttag = $name;
    }
    }
    function _tivoend_elemen t($parser, $name)
    {
    global $depth, $items;
    $depth--;
    }
    function _tivodata($pars er, $data)
    {
    $data = str_replace("%a mp;%", "&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 =)


  • m|sf|t

    #2
    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)


    Comment

    Working...