Reading XML8:21 with Alena Holligan
XML provides more details than you can convey using a simple object. You'll often find XML being used for things such as rss feeds for blogs or podcasts. We will look at some of the simple ways of working with XML data using built-in PHP functions.
When you need more control of your xml, you could try a package like sabre/xml.
Other XML with SVG
Scalable Vector Graphics (SVG) is an XML markup language for creating two-dimensional images using vectors. If you'd like to learn more about how to modify abd use SVGs. Check out our SVG Basics course.
There are also packages specifically designed to work with SVG, such as php-svg by JangoBrick to read, edit, write, and render SVG files with PHP
XML used to be king when it came to sharing data on the web, but 0:00 XML is a lot more complicated and people appreciate the ease of use of JSON. 0:04 But XML is also a lot more detailed. 0:09 You can convey a lot more than just a simple object. 0:13 As with so many things, one is not necessarily better than the other. 0:16 They're just different, because they're different, they have different uses. 0:21 You'll often find XML being used for things such as our assessed feeds for 0:25 blogs or podcasts. 0:29 I thought this would be a great opportunity to share some of my 0:31 favorite podcasts. 0:34 At the same time, we use some of PHP's built in functionality for 0:35 working with XML. 0:39 Like I said before, XML can be a lot more detailed and complicated. 0:41 So, if you're trying to do anything very complicated, I'd suggest you look for 0:46 a package that will give you a lot more features. 0:50 See the notes attached to this video for more information. 0:53 Open podcasts.php. 0:55 Once again. 0:58 I've started by showing the contents of an XML file. 0:59 Since XML uses tags, just like HTML does, 1:03 we need to use the htmlspecialchars function for these tags to be visible. 1:07 I have also included a sample podcast and an episode. 1:12 Let's preview this in a browser. 1:16 We can see that the XML file has a lot more details than the JSON file. 1:21 So it's not quite as clean, but we have a channel for the podcast with the details 1:26 about the podcast itself, and then items with details about individual episodes. 1:30 Let's go back to work spaces, remove the file display and 1:37 replace the sample podcast with the data from the XML. 1:41 For this example, I have multiple XML files, one for each podcast. 1:52 Let's use what we learned in the first section, to read the files from the XML 1:57 directory, and put them into an array first we'll create an empty array. 2:01 We'll call it files. 2:05 Then we'll set our directory. 2:11 If Opendir, 2:21 our directory And closedir. 2:27 Now we'll loop through the directory. 2:39 while (($entry=readdir($fh)) 2:42 !==false. 2:52 I don't want any files that start with a dot. 3:00 If substring, the entry, starting with the first character and 3:06 polling one character Does not equal dot. 3:12 Then we're going to add this entry to the files. 3:24 Directory slash entry. 3:33 Now we can loop through each of our files. 3:38 If not empty files. 3:44 For each $files as $file. 3:51 And then, we're going to use a simplexml load file function, 3:57 to parse the XML file into an object, xml = simplexml_ 4:02 load_file(file). 4:09 Now we're ready to replace the sample podcast with the details from our 4:15 XML file. 4:20 First we add our link. 4:43 XML channel link, 4:46 Title, Description. 5:04 Now I don't want to show every episode. 5:13 So instead, I'm going to pull a random 5:16 item. 5:19 Make sure that we subtract one from the count, since the rand range is inclusive. 5:35 Item random, And then the title. 5:52 Let's copy this for the rest of our episode. 6:03 Our audio source is a little tricky, Enclosure, 6:19 Attributes, And then, URL. 6:28 And we'll use the link to the podcast once more. 6:38 Let's take a look at what we have so far An error, let's go fix that. 6:44 We have a list of podcasts with a random episode to try, and a link to learn more. 7:02 When we refresh, we get a different episode for each podcast. 7:08 There's one more thing that I want to show you. 7:13 So let's go back to workspaces. 7:15 This is a great place to demonstrate pulling external files. 7:18 Any of the functions that read files can read any file from anywhere. 7:22 As long as you have access and correct permissions. 7:27 It doesn't matter if the files are local to your computer, on a local network or 7:30 even somewhere on the web. 7:35 Let's add another of my favorite podcasts. 7:38 Files. 7:51 Now let's view this in the browser again. 8:09 This new podcast was added, right along with all the others. 8:16
You need to sign up for Treehouse in order to download course files.Sign up