02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Loading XML in PHP

So far, we have seen examples of how applications on the client side in web-interactions load XML data that is rendered to them as the content format. We'll now take a look at PHP, a specialized server-side language that makes XML content available to client applications such as HTML browsers, which then render this information as text in the browser window using special languages like Javascript. We'll start with a very simple example, using samples of XML information as well as a PHP script to load this document onto the browser application:

<?

$xmlString = "<canine>who let the dogs out?</canine>";

echo $xmlString;

?>

When this information is fed to the browser, it displays the output "Who Let the Dogs out?" correctly. It displayed data because the root element has no nested elements inside of it. If we store an XML document instead that consists of nested elements, it doesn't make sense to display the contents of the root element on the browser. It's not a simple string any more. So it simply displays a blank page, with NO content.

<? $xmlString = "<canine>

<question>Who Let the dogs out?</question>

<answer>It's a dog's life!</answer>

</canine>"

echo $xmlString;

?>

In the next session we shall see what kind of PHP code needs to be written in order to parse any XML document and then give it to the client software (browser) so that only the content is correctly displayed. We can see the division of responsibilities between technologies and languages, all centered around content management. On the server side is PHP, and on the client side (mainly browsers,) it is Javascript. XML is the common glue that binds these technologies together, and provides a extensible and flexible framework for content management.

Video Transcript

00:00Alrighty, so I did my due diligence, and we have your three screens open up at the same
00:04time.
00:05We first have here our domain, LearnXMLNow.com, as it is at this stage while we're creating
00:10a course.
00:11We have our domain itself, our server, which supports PHP.
00:16And we have our editor, Smoltron, and I'm using Cyberduck, because it's a really great
00:21editor.
00:22Simple while I'm on a Mac, but you should work with whatever editor you feel like working
00:26with.
00:27If you find it hard to find an editor for this, contact us, and we'll be more than happy
00:33to help you in our commuted forums, on our Facebook chat, on pages, and so on and so forth.
00:39So let's look at our PHP file and just take a peek and see what's inside of it right now.
00:43What's really nice about this editor is I could edit the PHP, and it will automatically,
00:47as soon as I save the file, update it back onto the server so we could see live updates.
00:52Alright, so we did our intro to this, and now we're ready to start scripting some
00:56PHP.
00:57Now, like I said in our previous video, there's a lot of different ways of working
01:01with XML, and actually one of the most simple ways of doing it in PHP is a library that's
01:07called SimpleXML, and I want to show you how SimpleXML works.
01:13Now there's really two main ways how you'd be working with XML on the server side.
01:18You might be generating the XML yourself, and then trying to output that to the user,
01:24and you might be just wanting to fetch an XML that already exists to fetch information
01:28out of it.
01:29Both are legitimate.
01:30So let's look at the first tab and the second tab at the same time.
01:34First of all, let's imagine that we want to create an XML on our own, and let's
01:37imagine that we had a string that was that XML that we wanted.
01:41So let's create that string.
01:42So first of all, this is how we create a variable in PHP.
01:47If you don't know PHP, notice how we have it with this dollar sign.
01:50That's our definition of our variable.
01:52Without it, we would get into trouble.
01:55And I'm going to create my little sample XML.
01:57It's going to be very, very rudimentary, and I'm just going to have here some code
02:01that says Hello World, and I'm ready to do something, right?
02:07So first of all, I have a string.
02:08Now I really want to convert that into an XML so I could work with it.
02:13The way I convert it into a SimpleXML in PHP is really, really simple, so I'm going
02:17to create a variable, I'm going to call it SimpleSM or SX for SimpleXML, and I'm
02:23going to create a new SimpleXML element, and I'm going to send into it that string
02:31that we just created.
02:32Now that string could have came from anywhere, but really it's just a string,
02:37any string.
02:38Now obviously if I run it right now because we're running in PHP, we wouldn't
02:40see anything, so I really have to output something.
02:42Now because I created a really, really basic element, if I just try to echo
02:47that simple element, if I just try to echo it the way it is, it's just going to output
02:52that child because it's a simple node and we're actually already inside of it when
02:57we name it, right, because we're in the root as soon as we call it.
03:00So if I save this and just refresh my browser, we're going to see that, I'm going
03:05to see that Hello World, and we have here our first native, simple XML application.
03:10We're ready to start building and creating dynamically new XMLs and integrate them into
03:17our PHP.
03:18Now let's see a really, a more, you know, a more realistic scenario where instead
03:23of this, we'd actually have that XML on a file waiting for us, which is actually
03:28this file.
03:29If we click on our edit so we could see what's in that file, we'll see that
03:32I created a very, very simple XML file.
03:34It has a header, it has a header, and it has a title.
03:38So let's see how we could actually fetch that file.
03:40Very, very simple with simple XML.
03:43There's a very simple function.
03:45All I have to do is set my XML, and I could then just call the function
03:49simple XML load file with these underlines, underlines, simple XML,
03:57underline load, underline file, and all that's left is for me to name the
04:01name of the file that I want to load.
04:03In my case, it's info.xml.
04:07And there you go.
04:08After line seven, we already have an XML, and we could test this out and see
04:11if that's really true, and I could just echo out, let's echo out, let's see
04:17in our XML, if I just echo out our XML, I'm not going to get anything
04:20because this is a complicated element, which really is going to take us to
04:24our next topic, which is going to be how to actually access information
04:28inside of the XML.
04:29So now we're not getting anything, but we definitely have that XML.
04:32Only when we're trying to output it, we're trying to output all this,
04:35so automatically it's just returning to us nothing or emptiness.
04:40So we just covered two really major stuff.
04:43One we saw how to create our own XML ourself.
04:46If this was a simple XML, which had only a line of text right here, we would
04:50output it, but it's not right now, and obviously our next step is to see how
04:53to go to travel through the XML.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!