DEV Community

loading...

PHP XML Reader Example

skptricks profile image skptricks ・1 min read

Post Link : https://www.skptricks.com/2018/04/php-xml-reader-example.html

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.

PHP XML Reader Example

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

Discussion (1)

pic
Editor guide
Collapse
vlasales profile image
Vlastimil Pospichal • Edited

When using large XML with a DOMDocument...

$doc = new \DOMDocument('1.0', 'UTF-8');
$doc->load($filePath, LIBXML_PARSEHUGE);