Writing XML9:29 with Alena Holligan
We need to be able to update the xml data when a new episode comes out. Let's find out what it takes to control XML data with PHP.
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
We have most of the podcast stored locally. 0:00 That means if a new episode comes out we have to update the XML data ourselves. 0:03 Yes, you could just link to the RSS feed like we did with full stack radio. 0:08 But this is a lot slower to process and, of course, 0:13 this course is about taking control. 0:16 Let's find out what it takes to control the XML data with PHP. 0:18 Create another file in the inc directory. 0:23 We'll name this one write_xml. 0:28 We'll start by loading the podcast file we wish to modify into an XML object. 0:36 We'll be using the data from the add_episode form. 0:54 Simplexml_load_file and our file. 1:21 We want to add an additional item. 1:30 So, we're going to call the add child method on the XML channel. 1:33 AddChild('item'). 1:48 Then we can add our child details. 1:53 I'm going to create a new date object from the form input so 2:30 that I can format it properly. 2:34 Now, we can add the publication date. 3:04 Date -> format, the day name, 3:15 day, month, year, hour. 3:22 And then for the description, I'm going to copy the title again. 3:34 Let's write out what we have so far so we can preview this in the browser. 3:50 We can use the method asXML. 4:09 Simple XML doesn't really handle making the XML pretty or easy to read. 4:17 There are packages that can do this. 4:22 But we aren't really going to be looking at the code itself, so 4:24 it doesn't really matter. 4:27 It doesn't need line breaks to be valid XML. 4:29 But to make this a little easier for 4:31 us to read right now, let's add our own line breaks. 4:33 Anywhere the tags meet. 4:46 We're going to add a line break. 4:51 Now, let's preview this in a browser. 4:56 Let's pull the latest episode of Educate Yourself. 5:07 Add episode. 5:13 Choose the Educate Yourself. 5:15 And we'll enter the details. 5:21 Great, as you can see our new episode was added. 6:05 But there are a few more bits of information that I want to add. 6:08 XML offers many additional options for adding details. 6:12 I'm going to demonstrate one of them. 6:17 As you see in the previous episodes, there are some tags that start with iTunes and 6:19 a colon before the name of the tag. 6:24 This is the XML way of namespacing. 6:26 At the top of the file. 6:28 We see these xmlns for namespace. 6:32 And here's our iTunes namespace. 6:47 We'll copy this and go back to workspaces. 6:50 Itunes. 7:01 And we just need this URL. 7:10 Then we can add the namespace as the third parameter of the addChild method. 7:13 We'll copy this line for summary, explicit, and duration. 7:51 Now, let's preview these changes in the browser again. 8:18 Now, we see the iTunes information as well. 8:31 And we're ready to write this to a file. 8:33 Let's comment out this display. 8:39 Instead of echoing the asXML, 8:46 we can call xml->asXML and pass the file. 8:50 We'll then redirect. 8:58 To podcasts. 9:06 Back in the browser let's resubmit the form. 9:07 Since these are random, we can refresh until we see the new episode. 9:21 We could also specify a specific episode if we wanted too. 9:25
You need to sign up for Treehouse in order to download course files.Sign up