You are here:  » Dynamic XML file names


Dynamic XML file names

Submitted by thePOSTMAN on Wed, 2006-06-28 15:23 in

Hello,

Just curious if it is possible with Magic Parser to read from a directory where there will be numerous XML files with a different naming convention for each. The system we are using will generate a new XML file for item, so I need to be able to read all the XML files from the directory in which they are located.

Is that possible with Magic Parser?

Submitted by support on Wed, 2006-06-28 19:19

Hi,

As long as you know what filenames are in the directory (which you can discover with PHP's opendir() function) then it would be no problem to open the files individually.

Provided that all the XML files are of the same format, then you could use the same record handler function to process each one. Here's some example code to show what I mean:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    
// process $record, such as loading a database
  
}
  
$dir "path/to/xmlfiles";
  if (
$dh opendir($dir))
  {
    while ((
$file readdir($dh)) !== false)
    {
      
MagicParser_parse($file,"myRecordHandler","XML|FORMAT/STRING/");
    }
    
closedir($dh);
  }
?>

You need to consider how you are going to identify what core type each XML file is; unless there is an easy way to detect this in a field that is likely to be in every $record within each file. Remember that you can use the demo to try Magic Parser against your own files to check that it is going to work.

Hope this helps!
David.

Submitted by thePOSTMAN on Wed, 2006-06-28 21:33

David,

Thank you for your help.

By core type you mean:

<?php
MagicParser_parse
("12345.xml","myRecordHandler","xml|RESIDENTIALPROPERTY/");
?>

The xml|RESIDENTIALPROPERTY

Which is each one of the XML files. I know there are going to be problems with which one comes first and how they are listed, but for the time being I am just trying to get them all to show up. The code you posted reads pretty well, and I see the PHP opendir() but when I tryed it nothing worked.

Submitted by thePOSTMAN on Wed, 2006-06-28 21:39

David,

My mistake, I was pointing to the wrong directory.

Again, thank you David for your help with this.

Aaron