Validating large XML data against XSD in PHP

Surajudeen Akande
Published in
2 min readMar 22, 2017


It’s 2017 and you still prefer using XML(Extensible Markup Language) to JSON(JavaScript Object Notation) for data-interchange? Well, that is not the point about this article. Everything you want to say about the trade-offs of either of them has been dealt with here. But there are still a lot of systems using XML today and I can assure you that it would still be the case X years from now.

Validating XML against XSD might be the first step to take especially when building a feed Reader/Ingester. For Starters, any file like the sample below is a well-formed XML file.

and below is a sample XSD (XML Schema Definition) file.

Writing a XSD for your XML is actually easy to do, you make use of tools like freeformatter or do a crash course on w3Schools.

Now, we are ready to validate our XML file against the XSD using either DOMDocument or XMLReader. First, make sure that these extensions are enabled on your PHP installation.

Validating With DOMDocument

This DomValidator can be easily used like so:

The above piece of code is actually easy to understand, the most important method here is the validateFeeds() method.

Validating With XMLReader

The upside of using XMLReader over DomDocument is scalability. XMLReader can handle very large files better than DomDocument. Our class will be very similar to that of DomDocument. Also note that your libxml version is above 2.6.

We use this class too similar to how we used the DomValidator class.

So that’s it. I hope to follow this up with ingesting feeds very soon. You can reach me on for feedback, I will appreciate it.



Surajudeen Akande
Writer for

Apprentice, T-shaped, Goal-digger and Public Speaker