Re: Urgent: Way to XML-parse data with <f n="name"> </f> tags?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Myron Turner wrote:
Rob Gould wrote:

The problem I'm having is that the XML data that comes back from the host doesn't just have <eventname> </eventname> tags. It has <f n="eventname">datahere</f> tags, and I don't know how to get the XML parser to read the values using that format. (And I don't have control over the host) As a first step, I just want to retrieve the "eventname", "venuename", and "venuecity" data from within the "result" tags at the bottom of the data.

I'm really hoping this is possible with PHP - - - can someone please steer me in the right direction and tell my what I should change in the below script? The $tag value is not getting a value string, due to the XML-data....

Here's my present script:

<?php

$insideitem = false;
$tag = "";
$eventname = "";
$venuename = "";
$venuecity = "";

function startElement($parser, $name, $attrs) {
    global $insideitem, $tag, $eventname, $venuename, $venuecity;
    if ($insideitem) {
        $tag = $name;
           } elseif ($name == "RESULT") {
        echo "found result";
        $insideitem = true;
    }
}

?>

You'll get these from the attributes array, which is an associative array:

   The third parameter, /attribs/, contains an associative array with
   the element's attributes (if any).The keys of this array are the
   attribute names, the values are the attribute values.Attribute names
   are case-folded
   <http://www.php.net/manual/en/ref.xml.php#xml.case-folding> on the
   same criteria as element names.Attribute values are /not/ case-folded.

From: http://www.php.net/manual/en/function.xml-set-element-handler.php


So $venuecity - $attrs['venuecity'], etc.
--PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




OK. I was able to get hold of the file. Here's a small part of the listing:

  <f n="venueneighborhood">xphoenix;Paradise Valley;Greater Phoenix</f>
  <f n="eventname">Remembering Amy</f>
  <f n="eventsortname">REMEMBERING AMY</f>
  <f n="eventid">3288076</f>
  <f n="venueneighborhoodid">670;409;1957</f>
  <f n="venuestate">AZ</f>

What you want to do here is to process the file, as you were doing, checking for the attribute value you want, then capture the element data once the attribute name is found. Here's a way to do that (not tested but should work):

$eventname = "";
$venuename = "";
$venuecity = "";
$found = "";

function startElement($parser, $name, $attrs) {
  global  $eventname, $venuename, $venuecity;

 // speed up the parsing since all the values have been found
 if(!empty($eventname)  && !empty($eventname) && !empty($eventname)  {
       return;
 }

   $found = $attrs['n'];
}

function characterData($parser, $data) {
   global  $eventname, $venuename, $venuecity;

 // speed up the parsing since all the values have been found
 if(!empty($eventname)  && !empty($eventname) && !empty($eventname)  {
       return;
 }

 switch ($found) {
   case 'eventname':
      $eventname = $data;
   break;

   case 'venuename':
      $venuename = $data;
   break;
case 'venuecity':
      $venuecity = $data;
     break;
  }


}

--

_____________________
Myron Turner
http://www.room535.org
http://www.bstatzero.org
http://www.mturner.org/XML_PullParser/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux