使用 PHP XMLReader 解析 XML
我正在编写从 $_POST 变量读取一些 XML 的程序,然后使用 PHP XMLReader 进行解析,并将数据提取输入到数据库中。我使用 XMLReader 是因为提供的 XML 很可能太大而无法放入内存。
但是我遇到了一些问题,我的 XML 和基本代码如下:
' <数据根> <数据> <信息>值 <动作>值 '
$request = $_REQUEST['xml'];
$reader = new XMLReader();
$reader->XML($request);
while($reader->read()){
//processing code
}
$reader->close()
我的问题是,如果传递的 XML 没有 行,代码将完美工作,但是如果我包含它,并且当应用程序进入实时生产环境时它将包含它,则 while 循环的
$reader->read()
代码不起作用,并且不会解析 XML在 while 循环内。
有没有人以前见过类似的行为或知道为什么会发生这种情况?
提前致谢。
I am writing program that reads in some XML from the $_POST variable and then parses using the PHP XMLReader and the data extracted input into a database. I am using the XMLReader as the XML supplied will more than likely be too big to place into memory.
However I am having some issues, my XML and basic code as are follows:
'<?xml version="1.0"?>
<data_root>
<data>
<info>value</info>
</data>
<action>value</action>
</data_root>'
$request = $_REQUEST['xml'];
$reader = new XMLReader();
$reader->XML($request);
while($reader->read()){
//processing code
}
$reader->close()
My problem is that the code will work perfectly if the XML being passed does not have the <?xml version="1.0"?>
line, but if i include it, and it will be included when the application goes into a live production environment, the $reader->read()
code for the while loop does not work and the XML is not parsed within the while loop.
Has anyone seen similar behaviour before or knows why this could be happening?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的意思是:“不起作用”?您遇到任何错误吗?
[编辑] ...
我也可以重现你的问题,我尝试了与 VolkerK 完全相同的事情:
它产生:
1 - 14 - 1 - 14 - 1 - 3 - 15 - 14 - 15 - 14 - 1 - 3 - 15 - 14 - 15 -
我用过:
PHP 5.3.2-0.dotdeb.1 与 Suhosin-Patch (cli)(构建时间:2010 年 3 月 9 日 10:14:53)
What do you mean with: "does not work"? Are you getting any errors?
[edit] ...
I can reproduce your problem either, I tried the exact same thing as VolkerK:
which produces:
1 - 14 - 1 - 14 - 1 - 3 - 15 - 14 - 15 - 14 - 1 - 3 - 15 - 14 - 15 -
I used:
PHP 5.3.2-0.dotdeb.1 with Suhosin-Patch (cli) (built: Mar 9 2010 10:14:53)
我无法重现该行为(使用 php 5.3.2/win32 + firefox 作为第二个示例的客户端)。
打印
1 14 1 14 1 3 15 14 15 14 1 3 15 14 15
。$_REQUEST['xml'] 真的包含您期望的内容吗?
编辑:或者另一个实际
再次使用 _REQUEST 的示例
1 14 1 14 1 3 15 14 15 14 1 3 15 14 15
在提交表单时打印。I cannot reproduce the behavior (using php 5.3.2/win32 + firefox as the client for the second example).
prints
1 14 1 14 1 3 15 14 15 14 1 3 15 14 15
.Does $_REQUEST['xml'] really contain what you expect it to?
edit: Or another example that actually uses _REQUEST
again
1 14 1 14 1 3 15 14 15 14 1 3 15 14 15
is printed when the form is submitted.您需要确认一些事情:
此外,您可能应该使用 $_POST 检索该数据,只是一个更好的做法。
You'll need to confirm a few things:
Also, you should probably be using $_POST to retrieve that data, just a better practice.
好的,这里主要的红脸编码员,对我的开发环境做了一些更改,我昨天安装了新版本的 php,并且 magic_quotes_gpc 设置为“on”,从而转义了 XML 中的引号并导致了问题,
谢谢您的帮助
ok, major redfaced coder here, having made some changes to my dev environment, i installed a new version of php yesterday, and magic_quotes_gpc was set to 'on' thus escaping the quotes in the XML and causing the problem
thank you for your assistance