替代 php4 中的 simplexml_load_string()
我在我的文件中使用这个函数 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()
的函数,这样我就可以避免更改编码。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我自己没有使用过这个类,但是 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
当人们告诉我更新我的环境而不是仅仅回答我的问题时,我自己很讨厌,但是没有办法切换到 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.
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.