This is a continuation of previous article, where I wrote on how to use PHP’5 DOM to read XML Files easily. But, for large files, its better to use XMLReader. Unlike DomDocument, XMLReader does not load the entire File on memory. It reads an XML file one node at a time. I hardly use XMLReader because it requires writing a lot of codes, but for extremely large XML Files, its better to use it. Full reference on XMLReader can be viewed here. It requires PHP 5.2.
The following are the functions I use:
- open – opens XML File
- read – reads node of XML
- getAttribute – gets the attribute of a node
- moveToNextAttribute – moves to next Attribute
- next – moves to next node
Here’s a Basic XML File called ‘test.xml’:
<xml version="1.0" encoding="ISO-8859-1"> <library> <book isbn="781"> <name>SCJP 1.5</name> <info><![CDATA[Sun Certified Java Programmer book]]></info> </book> <book isbn="980"> <name>jQuery How To</name> <info><![CDATA[jQuery Reference Book]]></info> </book> <library>
Below is a way to read it:
At first initialize and open the XML file
$xmlReader = new XMLReader(); // open the file for reading $xmlReader->open('test.xml')
Now keep reading nodes until the end has been reached, which done by a while loop:
while($xmlReader->read()) { }
Below is the full code:
$bookList = array(); $i=0; $xmlReader = new XMLReader(); $xmlReader->open('test.xml'); while($xmlReader->read()) { // check to ensure nodeType is an Element not attribute or #Text if($xmlReader->nodeType == XMLReader::ELEMENT) { if($xmlReader->localName == 'book') { $bookList[$i]['book_isbn'] = $xmlReader->getAttribute('isbn'); } if($xmlReader->localName == 'name') { // move to its textnode / child $xmlReader->read(); $bookList[$i]['name'] = $xmlReader->value; } if($xmlReader->localName == 'info') { // move to its textnode / child $xmlReader->read(); $bookList[$i]['info'] = $xmlReader->value; $i++; } } }
Here’s a var_dump of $bookList
array(2) { [3]=> array(3) { ["book_isbn"]=> string(3) "781" ["name"]=> string(8) "SCJP 1.5" ["info"]=> string(34) "Sun Certified Java Programmer book" } [4]=> array(3) { ["book_isbn"]=> string(3) "194" ["name"]=> string(18) "jQuery is Awesome!" ["info"]=> string(21) "jQuery Reference Book" } }
That’s about it. It requires writing a lot of codes, but it’s useful for large (by large I mean extremely large) XML files.
However, when the XML files is very complex (and not extremely large), I find both DomDocument or XMLReader are not ideal solution. I rather use XPath (DomXPath), which I will hopefully write in my next article.