tags:

views:

411

answers:

4

Hi Guys,

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.

A: 

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)

hoppa
The code within the while loop is not executing, its as if the read() function is not calling correctly. I have verified by letting the whole program execute, and by placing a `die()` within the while loop that is never accessed.
A: 

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.

VolkerK
mmm if i paste my XML into the code as in the first example it works perfectly but if it is coming through from a previous page and a text area like example 2, or when pasted into the URL it is not working, im very mystified by this
A: 

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.

Rabbott
Also have a look at the SimpleXml options - the simplexml_load_file is another option, which you may have better luck with.
Rabbott
A: 

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

If it is your development server set error\_reporting=E\_ALL (or higher), too. You would have gotten warning messages from the xmlreader about the malformed document then ;-)
VolkerK