Support forum login

©2006-2014 IAAI Software

Contact Us Privacy Policy

Universal PHP XML & CSV Parser Library

Parse just about any XML or CSV variant (comma, tab, pipe separated) text file, string or URL in PHP, ideal for XML to CSV conversion, reading very large XML documents containing repeating records such as affiliate product feeds, syndication feeds, SOAP envelopes and response documents from XML web service APIs.

Click here to upload and try Magic Parser with your own data...

Description

bool MagicParser_parse ( string filename, callback record_handler [, string format] )

MagicParser_parse() reads and parses filename, calling the user specified function record_handler for every record in the file, or until record_handler returns a non-zero value. MagicParser_parse() will attempt to automatically detect the file type and data representation format unless this feature is overridden by the optional 3rd paramater format.

filename can be anything that would be used with PHP's fopen() function, including Internet URLs if URL wrappers are enabled. Magic Parser also has a built in wrapper for parsing data from a string instead of opening a file or URL. To use the wrapper, simply prefix your data with "string://".

record_handler is the name of a user defined function that will be called for each record in the file with a single parameter containing the current record as an associative array. For XML files, the key values contain a simple path style expression indicating the name of the element. Key values for element attributes have the attribute name appended by a hyphen. Potential duplicate key values are resolved by appending @1,@2 .. @n to the element name. For CSV files with a header row, key values are the field names contained in the header row. For CSV files with no header row, key values are FIELD1,FIELD2 .. FIELDn. If record_handler returns a non-zero value Magic Parser will stop reading the file.

format (optional) contains a string that describes the file type and other data representation parameters, thus avoiding the autodetection phase of the parse function. See the format string specification page for full details. For advanced applications, MagicParser_getFormat() can be used to return the format string derived by the autodetection logic. Common values can be found in the format string library.

Example 1

Parse the local file data.xml, using PHP's print_r() function to display the contents of each record. In this example, Magic Parser will autodetect the format of the source file.

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    
print_r($record);
  }
  
MagicParser_parse("/path/to/data.xml","myRecordHandler");
?>

Example 2

Given the following XML document pets.xml:

<pets>
  <pet>
    <name>Bonzo</name>
    <type>Dog</type>
  </pet>
  <pet>
    <name>Fluffy</name>
    <type>Cat</type>
  </pet>
</pets>

..the following PHP code will display a list of pet's names:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($pet)
  {
    print 
"<p>".$pet["NAME"]."</p>";
  }
  
MagicParser_parse("pets.xml","myRecordHandler","xml|PETS/PET/");
?>

Example 3

Parse the XML data in the string $myXML and display the match name and league number. This example shows how to extract attribute values from elements within an XML document:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    print 
$record["MATCH"] . " in league " $record["MATCH-LEAGUE"];
  }
  
$myXML "<results>
              <match league='1'>Harriers V Falcons</match>
            </results>"
;
  
MagicParser_parse("string://".$myXML,"myRecordHandler","xml|RESULTS/");
?>

Return Value

MagicParser_parse() returns true on success. A return value of false indicates that an error occurred. Use MagicParser_getErrorMessage() to determine the cause of the problem.