PHP Classes

xml with variable numbers of tags

Recommend this page to a friend!

      Generic XML parser class  >  All threads  >  xml with variable numbers of tags  >  (Un) Subscribe thread alerts  
Subject:xml with variable numbers of tags
Summary:How to read xml with variable numbers of tags
Messages:2
Author:Torsten Singleboerse-Tipp
Date:2013-05-01 21:42:34
Update:2013-05-02 03:36:49
 

  1. xml with variable numbers of tags   Reply   Report abuse  
Picture of Torsten Singleboerse-Tipp Torsten Singleboerse-Tipp - 2013-05-01 21:42:34
Hi,
i have a xml, in that some tags ar not at all products.

<products>
<product>
<name>pr1</name>
<ean>1234</ean>
<description>this is one product</description>
</product>
<product>
<name>pr2</name>
<description>this is one product</description>
</product>
<product>
<name>pr3</name>
<ean>12334</ean>
<description>this is one product</description>
</product>
</products>


How to read this file? (The file is very big, about 230MB)

Here is the code i use:
require('xml_parser.php');

$file_name = 'otto_roh.xml';
$error = XMLParseFile($parser, $file_name, 1, $file_name.'.cache');
if(strlen($error) == 0)
{
/*
* The types array defines the types and structure of the document
* to be validated before extracting its data values.
*/
$content_types = array(
'products'=>array(
'type'=>'hash',
'types'=>array(
'product'=>array(
'type'=>'hash',
/* allow tag to appear an unlimited number of times */
'maximum'=>'*',
'types'=>array(
'name'=>array(
/* The default maximum and minimum times is 1 for required tags */
'type'=>'text',
),
'ean'=>array(
'type'=>'text',
),
'description'=>array(
'type'=>'text',
),
)
)
)
)
);


And where i can make the sql statements to write the data from the xml to my database? I cannot read the whole xml file at once, because it is 230MB.

Best regards
Torsten



  2. Re: xml with variable numbers of tags   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2013-05-02 03:36:49 - In reply to message 1 from Torsten Singleboerse-Tipp
I am afraid this class would not be appropriate to parse large XML files because it builds the data structure in memory (class variables).

You can raise the configured PHP memory limit, otherwise you would need a different approach to read and parse the XML file in small chunks so it does not exceed the PHP memory limit.