PHP XML Reader Example
skptricks Aug 18
Today, In this tutorial we are going to learn how to parse an large XML file using XMLReader Class. The XMLReader extension is an XML Pull parser. The reader acts as a cursor going forward on the document stream and stopping at each node on the way.
The XMLReader extension was initially a PECL extension for PHP 5. It was later moved to the PHP source bundled as of PHP 5.1.0, and later enabled by default as of PHP 5.1.2
Advantages XMLReader :
It is faster since it is not loading the whole XML into memory.
It can parse large and high complex XML document having more sub-trees.
XML Reader Features:
Retrieving portion of XML document based on current node.
Getting attributes based on index, name or namespace.
Parsing elements based on attribute’s index, name or namespace.
Validating XML document
Lets see the example where we are going to parse an xml tag from the external source file. Here we are using XMLReader to get to each node, then use SimpleXML to access them. This way, you keep the memory usage low because you're treating one node at a time and you still leverage SimpleXML's ease of use.
Download link : PHP XML Reader Example