XML used to be king when it came to sharing data on the web, but
XML is a lot more complicated and people appreciate the ease of use of JSON.
But XML is also a lot more detailed.
You can convey a lot more than just a simple object.
As with so many things, one is not necessarily better than the other.
They're just different, because they're different, they have different uses.
You'll often find XML being used for things such as our assessed feeds for
blogs or podcasts.
I thought this would be a great opportunity to share some of my
favorite podcasts.
At the same time, we use some of PHP's built in functionality for
working with XML.
Like I said before, XML can be a lot more detailed and complicated.
So, if you're trying to do anything very complicated, I'd suggest you look for
a package that will give you a lot more features.
See the notes attached to this video for more information.
Open podcasts.php.
Once again.
I've started by showing the contents of an XML file.
Since XML uses tags, just like HTML does,
we need to use the htmlspecialchars function for these tags to be visible.
I have also included a sample podcast and an episode.
Let's preview this in a browser.
We can see that the XML file has a lot more details than the JSON file.
So it's not quite as clean, but we have a channel for the podcast with the details
about the podcast itself, and then items with details about individual episodes.
Let's go back to work spaces, remove the file display and
replace the sample podcast with the data from the XML.
For this example, I have multiple XML files, one for each podcast.
Let's use what we learned in the first section, to read the files from the XML
directory, and put them into an array first we'll create an empty array.
We'll call it files.
Then we'll set our directory.
If Opendir,
our directory And closedir.
Now we'll loop through the directory.
while (($entry=readdir($fh))
!==false.
I don't want any files that start with a dot.
If substring, the entry, starting with the first character and
polling one character Does not equal dot.
Then we're going to add this entry to the files.
Directory slash entry.
Now we can loop through each of our files.
If not empty files.
For each $files as $file.
And then, we're going to use a simplexml load file function,
to parse the XML file into an object, xml = simplexml_
load_file(file).
Now we're ready to replace the sample podcast with the details from our
XML file.
First we add our link.
XML channel link,
Title, Description.
Now I don't want to show every episode.
So instead, I'm going to pull a random
item.
Make sure that we subtract one from the count, since the rand range is inclusive.
Item random, And then the title.
Let's copy this for the rest of our episode.
Our audio source is a little tricky, Enclosure,
Attributes, And then, URL.
And we'll use the link to the podcast once more.
Let's take a look at what we have so far An error, let's go fix that.
We have a list of podcasts with a random episode to try, and a link to learn more.
When we refresh, we get a different episode for each podcast.
There's one more thing that I want to show you.
So let's go back to workspaces.
This is a great place to demonstrate pulling external files.
Any of the functions that read files can read any file from anywhere.
As long as you have access and correct permissions.
It doesn't matter if the files are local to your computer, on a local network or
even somewhere on the web.
Let's add another of my favorite podcasts.
Files.
Now let's view this in the browser again.
This new podcast was added, right along with all the others.