Parsing large XML documents using XMLReader / PHP5

Discussion in 'PHP' started by ska_mna, Nov 9, 2007.

  1. #1
    Hi guys,

    Would appreciate any pointers on how to read a large XML file from a URL. I've tried SimpleXML but due to shared hosting contraints I'm running into memory and timeout constraints. I think I need a stream reader but the documentation on XMLReader in PHP5 is a bit lacking and I can't find many examples. I need to get from an XML file like this;

    
    <?xml version="1.0" encoding="ISO-8859-1"?> 
    <products> 
    <product> 
    <name><![CDATA[]]></name > 
    <price><![CDATA[]]></price > 
    <description><![CDATA[]]></description > 
    </product> 
    etc.
    
    Code (markup):
    To an SQL insert like this (note I'll insert records in batches of perhaps 100);

    
    INSERT INTO products (name, price, description) 
    VALUES 
    (x, y, z) 
    (a, b, c) 
    etc. 
    
    Code (markup):
    I've got XMLReader working to an extent with this;

    
    $reader = new XMLReader(); 
    $reader->open($url); 
    while ($reader->read()) 
    { 
       echo $reader->name.": ".$reader->value."<br />"; 
    }
    
    PHP:
    But I'm not sure on the most efficient way of going through the nodes and parsing them all into their respective components.

    An example of how to do this would be great.

    Thanks.
     
    ska_mna, Nov 9, 2007 IP
  2. ska_mna

    ska_mna Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Sorry for bumping, but still stuck with this issue. Has anyone got any ideas on reading large XML documents into a database as in the above scenario?
     
    ska_mna, Nov 10, 2007 IP