使用 PHP XMLReader 解析 XML

发布于 2024-08-25 07:15:15 字数 704 浏览 13 评论 0原文

我正在编写从 $_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 技术交流群。

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

发布评论

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

评论(4

知足的幸福 2024-09-01 07:15:15

你的意思是:“不起作用”?您遇到任何错误吗?

[编辑] ...

我也可以重现你的问题,我尝试了与 VolkerK 完全相同的事情:

$r = new XMLReader();
$x = '<?xml version="1.0"?> <data_root>  <data>  <info>value</info>  </data> <action>value</action> </data_root>';
$r->XML($x);
while ($r->read()) { echo $r->nodeType . " - "; }

它产生:
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:

$r = new XMLReader();
$x = '<?xml version="1.0"?> <data_root>  <data>  <info>value</info>  </data> <action>value</action> </data_root>';
$r->XML($x);
while ($r->read()) { echo $r->nodeType . " - "; }

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)

走过海棠暮 2024-09-01 07:15:15

我无法重现该行为(使用 php 5.3.2/win32 + firefox 作为第二个示例的客户端)。

$request = '<?xml version="1.0"?> <data_root>  <data>  <info>value</info>  </data>  <action>value</action> </data_root>';
$reader = new XMLReader();
$reader->XML($request);
while($reader->read()){
  echo $reader->nodeType, " ";
}
$reader->close();

打印 1 14 1 14 1 3 15 14 15 14 1 3 15 14 15
$_REQUEST['xml'] 真的包含您期望的内容吗?

编辑:或者另一个实际

<?php
if ( isset($_REQUEST['xml']) ) {
  $request = $_REQUEST['xml'];
  $reader = new XMLReader();
  $reader->XML($request);
  while($reader->read()){
    echo $reader->nodeType, " ";
  }
  $reader->close();
  die;
}
$pre = htmlspecialchars(
'<?xml version="1.0"?>
  <data_root>
    <data> 
      <info>value</info>
    </data> 
  <action>value</action>
</data_root>');
?>
<html><head><title>....</title></head><body>
  <form method="post" action="?">
    <div>
      <textarea cols="25" rows="8" name="xml"><?php echo $pre; ?></textarea>
      <br />
      <input type="submit" />
    </div>
  </form>
</body></html>

再次使用 _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).

$request = '<?xml version="1.0"?> <data_root>  <data>  <info>value</info>  </data>  <action>value</action> </data_root>';
$reader = new XMLReader();
$reader->XML($request);
while($reader->read()){
  echo $reader->nodeType, " ";
}
$reader->close();

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

<?php
if ( isset($_REQUEST['xml']) ) {
  $request = $_REQUEST['xml'];
  $reader = new XMLReader();
  $reader->XML($request);
  while($reader->read()){
    echo $reader->nodeType, " ";
  }
  $reader->close();
  die;
}
$pre = htmlspecialchars(
'<?xml version="1.0"?>
  <data_root>
    <data> 
      <info>value</info>
    </data> 
  <action>value</action>
</data_root>');
?>
<html><head><title>....</title></head><body>
  <form method="post" action="?">
    <div>
      <textarea cols="25" rows="8" name="xml"><?php echo $pre; ?></textarea>
      <br />
      <input type="submit" />
    </div>
  </form>
</body></html>

again 1 14 1 14 1 3 15 14 15 14 1 3 15 14 15 is printed when the form is submitted.

网名女生简单气质 2024-09-01 07:15:15

您需要确认一些事情:

  1. 数据库中的列类型足够大以容纳 xml,如果不是,它只会存储其中的一部分,从而导致无效的 xml“文档”并导致 XML解析器失败。
  2. 检查 php.ini 文件夹以确保您的 post_max_size、max_upload_filesize 和 memory_limit 值足够大,如果不是,则会出现相同的问题,php 无法解析它,因为它是无效的“文档”

此外,您可能应该使用 $_POST 检索该数据,只是一个更好的做法。

You'll need to confirm a few things:

  1. That your column type within your database is large enough to hold the xml, if it is not it will only store part of it, resulting in a non valid xml 'document' and causing the XML parser to fail.
  2. Check you php.ini folder to make sure that your post_max_size, max_upload_filesize, and memory_limit values are large enough, if they are not, the same issue arrises, php cannot parse it because it is an invalid 'document'

Also, you should probably be using $_POST to retrieve that data, just a better practice.

还不是爱你 2024-09-01 07:15:15

好的,这里主要的红脸编码员,对我的开发环境做了一些更改,我昨天安装了新版本的 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

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