From the course: Learning the Standard PHP Library

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Extracting data from XML with RegexIterator and SimpleXML

Extracting data from XML with RegexIterator and SimpleXML - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

Extracting data from XML with RegexIterator and SimpleXML

- This is courses.xml in the common data directory. It contains details of PHP courses in the lynda.com online training library. The root element is called courses, and then each individual top-level element is called course. And each course has a series of sub-elements such as title, author, description, level and so on. We can easily extract data from this file using SimpleXML and then filter it using RegexIterator. So let's create a new PHP file in the site root. We'll call it simplexml.php. We can load the XML using the SimpleXML load file function. So we'll call it courses, simplexml<u>load</u>file and it's the path common/data/courses.xml. This creates an instance of SimpleXML element and it contains all of the data from that file. And we can extract it using a foreach loop. So foreach courses as course and inside the loop, course represents one of the top-level elements in that XML document. I've given the variables the same names as the root elements and the top-level…

Contents