You are here:  » Whitespace at top of XML file


Whitespace at top of XML file

Submitted by jbregman on Sun, 2006-04-23 10:42 in

I have an XML file that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<lastmod>10:58:33</lastmod>
<stat>
<name>Bookings</name>
<type>Int</type>
<value>28</value>
</stat>
<stat>
<name>Avg Pick</name>
<type>Float</type>
<value>8.5</value>
</stat>
</dataset>

The XML file is produced by a crystal report. As such there are some lines of whitespace at the top of the file which can't be avoided. If I open the file in IE, for example, it picks it up fine.

However, when I try this

<?php
 
$xml 
= array();
function 
myRecordHandler($record) {
    global 
$xml;
    
$xml[] = $record;
}
$result MagicParser_parse("test.xml","myRecordHandler");
if (!
$result) {
    print 
MagicParser_getErrorMessage();
}
print_r($xml);
?>

I get

The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.

--------------------------------------------------------------------------------

Invalid at the top level of the document. Error processing resource 'http://pete.ecourier.co.uk/xml.php'. Line 1, Position...

Array
^

Have tried using autodetection but "XML/Dataset/" doesn't seem to work.

Help!

Submitted by support on Sun, 2006-04-23 10:50

Hi There,

White space will cause autodetection to fail; but the parse should still work with the correct format string, which in the case of your example XML should be:

xml|DATASET/STAT/

Secondly, i'm not sure why your example script is being interpretted by the browser as XML; as you are not creating XML headers at all.

However, that said; the following code should work; and your print_r() statement at the end should print out an array of each STAT from the XML file...

<?php
  $stats 
= array();
  function 
myRecordHandler($record) {
    global 
$stats;
    
$stats[] = $record;
  }
  
$result MagicParser_parse("test.xml","myRecordHandler","xml|DATASET/STAT/");
  if (!
$result) {
    print 
MagicParser_getErrorMessage();
  }
  
print_r($stats);
?>

Let me know if that code works as it stands..

Cheers,
David.

Submitted by jbregman on Sun, 2006-04-23 10:58

Hi David,

Unfortunately that doesn't work. It just returns:

Array ( )

Submitted by support on Sun, 2006-04-23 11:10

Sorry about that - i've been able to replicate the problem now. I'll investigate the circumstances there, but in the mean time you should be able to use the createFile function to create a version of the file with white space stripped. The following code should work:

<?php
  
require("MagicParser.php");
  
$stats = array();
  function 
myRecordHandler($record) {
    global 
$stats;
    
$stats[] = $record;
  }
  
// read XML into a string
  
$fp fopen("test.xml","r");
  while(!
feof($fp)) $xml .= fread($fp,1024);
  
fclose($fp);
  
// remove white space
  
$xml trim($xml);
  
// parse using temporary file API
  
$temp MagicParser_createFile($xml);
  
$result MagicParser_parse($temp,"myRecordHandler","xml|DATASET/STAT/");
  if (!
$result) {
    print 
MagicParser_getErrorMessage();
  }
  
print_r($stats);
?>

Submitted by jbregman on Sun, 2006-04-23 11:28

That works, thanks. But of course it would be better and quicker not to have to trim the file--can you let me know what you find as to why it doesn't work?

Cheers,

Jay

Submitted by support on Sun, 2006-04-23 11:40

Will do, Jay.

Cheers,
David.

Submitted by jbregman on Mon, 2006-05-01 17:24

David,

Any luck yet in determining / fixing the root cause of the problem?

Cheers,

Jay

Submitted by jbregman on Fri, 2006-05-12 08:15

David,

How about some follow up here please???

Jay

Submitted by support on Fri, 2006-05-12 08:21

Hello Jay,

My apologies for overlooking this thread.

As the file is technically not valid XML it will require an update to the script in order trim white space where an XML format string is provided. For performance reasons I would rather not incorporate this into the distribution, but I would be happy to create a customised version that should remove the need to download the file to your server and perform the trim at the start of the parsing process.

I'll drop you an email shortly with the new version.

Cheers,
David.

Submitted by support on Fri, 2006-05-12 09:05

Hello Jay,

I have actually decided to incorporate leading white space removal into the script, so you can now download the latest version to avoid having to create the temporary file.

The example from this thread would now be as follows:

<?php
  
require("MagicParser.php");
  
$stats = array();
  function 
myRecordHandler($record) {
    global 
$stats;
    
$stats[] = $record;
  }
  
$result MagicParser_parse("test.xml","myRecordHandler","xml|DATASET/STAT/");
  if (!
$result) {
    print 
MagicParser_getErrorMessage();
  }
  
print_r($stats);
?>

Hope this helps,
Regards,
David.