替代 php4 中的 simplexml_load_string()

发布于 2024-08-11 16:08:49 字数 2569 浏览 6 评论 0 原文

我在我的文件中使用这个函数 simplexml_load_string() ,它基于 lucene 进行搜索。 但它在 php4 中不起作用。

请建议我一个与此类似的功能,这样我就不必对我的编码进行太多更改。

这是我的代码示例:-

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;
    }

这里 $data 包含 xml 格式的数据,

<?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>

请建议一些类似于 PHP4 中的 simplexml_load_string() 的函数,这样我就可以避免更改编码。

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 suggest some function similar to simplexml_load_string() in PHP4 so that I can be escaped from changing my coding.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

oО清风挽发oО 2024-08-18 16:08:49

我自己没有使用过这个类,但是 phpClasses.org 正在为 PHP4 提供一个 simpleXML 实现,看起来相当不错:

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

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

不再见 2024-08-18 16:08:49

当人们告诉我更新我的环境而不是仅仅回答我的问题时,我自己很讨厌,但是没有办法切换到 PHP5 吗? PHP4 现在确实已经过时了,在我看来,它不再适合新项目了。 PHP5 带来了原生 XML 支持,而 PHP4 解决方案都相当慢。

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.

陈独秀 2024-08-18 16:08:49

PHP4 不支持异常。尝试删除 try...catch 块。
编辑: 另外(归功于下面的评论),SimpleXML 扩展仅适用于 PHP5。在 PHP4 中,您应该使用 XML Parser 扩展,但这也意味着您必须大量更改代码。一个简单的 Google 搜索 会提供一个指向 有关如何使用此扩展的教程

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文