tags:

views:

230

answers:

3

Im using this function simplexml_load_string() in my file which is doing search lucene based. But its not working in php4.

please suggest me a function similar to this one so that i dont have to change my coding much.

this is the sample of my code :-

function handleResponse($data) 
    {
     if ($data) 
     {
      $xml = simplexml_load_string($data);
      $results = array();
      foreach ($xml->result->doc as $story) 
      {
       $xmlarray = array();
       try
       {
        foreach ($story as $item) 
        {
         $name = $item->attributes()->name;
         $value = $item;
         $xmlarray["$name"] = "$value";
        }
       } 
       catch (Exception $e) 
       {
        echo 'Problem handling XML array.'; 
       }
       if ($this->debug) echo "checking if ".$xmlarray['score']." > ".$this->min_score;
       if ($xmlarray['score'] > $this->min_score) $results[] = $xmlarray;
      }
      $results['response']=$xml->result->attributes()->numFound;
     }
     else
     {
      $results=false;
     }
     return $results;
    }

here $data is having data in xml format like

<?xml version="1.0" encoding="UTF-8"?>
<response>

<lst name="responseHeader">
 <int name="status">0</int>
 <int name="QTime">2</int>
 <lst name="params">
  <str name="explainOther"/>
  <str name="fl">*,score</str>

  <str name="indent">on</str>
  <str name="start">0</str>
  <str name="q">reliance</str>
  <str name="hl.fl"/>
  <str name="qt">dismax</str>
  <str name="wt">dismax</str>

  <str name="version">2.2</str>
  <str name="rows">10</str>
 </lst>
</lst>
<result name="response" numFound="5" start="0" maxScore="1.7840868">
 <doc>
  <float name="score">1.7840868</float>
  <str name="scheme_name">Reliance Diversified Power Sector Fund - Growth</str>

  <str name="scheme_nav">75.23</str>
 </doc>
 <doc>
  <float name="score">1.7840868</float>
  <str name="scheme_name">Reliance Diversified Power Sector Fund - Growth</str>
  <str name="scheme_nav">75.23</str>
 </doc>
</result>
</response>

please suugest some function similar to simplexml_load_string() in php4 so that i can be escaped from changing my coding.

A: 

PHP4 does not support exceptions. Try removing the try...catch block.
Edit: also (credit goes to the comment below), the SimpleXML extension is PHP5-only. In PHP4 you should use the XML Parser extension, but that also means you will have to change your code a lot. A simple Google search gives a link to a tutorial on how this extension is used.

Ignas R
I think he is referring to the simplexml functions which are not native in PHP4.
Pekka
... which doesn't mean you're not right, though.
Pekka
removing this wont help me as this function is not working :-(
developer
A: 

I hate it myself when people tell me to update my environment instead of just answering my question, but is there no way to switch to PHP5? PHP4 is really, really outdated by now and in my mind not fit for new projects any more. PHP5 brings native XML support, while the PHP4 solutions are all quite slow.

Pekka
i agree and we'll be migrating to the new one but that will take some time and this work of mine needs to be made live now only.Thats why iam asking this question.
developer
I can't begin to imagine how much money that's going to waste. Running PHP 4 instead of PHP 5.3 wastes considerable server resources, plus the time spent converting that perfectly valid code to that very very old version of PHP, plus fixing the bugs you're going to encounter because your PHP 4 workaround can't implement SimpleXML's iterators and the mysterious bugs that'll show up every now and then because of PHP 4's by-value paradigm.I hope you bill by the hour.
Josh Davis
+1 all valid points...
Pekka
+2  A: 

I have not used this class myself, but phpClasses.org is sporting a simpleXML implementation for PHP4 that looks quite ok:

http://www.phpclasses.org/browse/package/4484.html

Pekka