02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous Video
Chapter is concluded.Next is

Modifying XML

Having a repository of information is important; updating it frequently after additions and deletions is even more so. This applies not only to repositories like databases, but also configuration data and structured information units that are maintained as XML documents. For server-side updates of information, PHP provides an API to do the same for XML documents. A real-world example that follows will help in getting a good idea of this concept.

Consider the following scenario: every day a few thousand babies are born in the USA. Since babies born in this country are automatically citizens by birth, a lot of information (birth certificates, Social Security information etc.) needs to be created and maintained at several levels, starting with the database in the city of birth. The records for babies born in NYC, for example, are created on a daily basis. One such document would look like:

<?xml version="1.0" encoding="UTF-8" ?>

<daily-births city="NYC" date="11-29-2012">

<baby>

<time>00:00:12</time>

<name>Sandy</name>

<sex>F</sex>

</baby>

<baby>

<time>00:00:21</time>

<name>Chris</name>

<sex>M</sex>

</baby>

...

</daily-births>

Now suppose that new entries need to be added to this XML document whenever a new baby is born. We could write a PHP script to do just that!

<? $xml = simplexml_load_file("newborns.xml");

$baby = $xml->addChild('baby');

$time = $baby->addChild('time', "00:00:12");

$name = $baby->addChild('name', "Cindy");

$sex = $baby->addChild('sex', "F");

echo $xml->asXML();

?>

The above piece of code is very intuitive and simple to visualize. Note that we have indented the code statements so that it resembles the nesting of elements in the XML document that we are accessing. Kind of makes it more readable, don't you think?

It is also possible to update current information with new values for element attributes and content. For example, suppose that after a baby named Sandy was born, her parents decided to rename her (as the father insisted she be named after his mother!) Suppose they decided to call her Heloise. PHP code to perform this update would look like:

<?

$xml = simplexml_load_file("newborns.xml");

$xml->baby[2]->name = "Heloise";

echo $xml->asXML();

?>

Voila! It's really simple, isn't it? PHP code is very readable, so don't be surprised if you feel at home immediately. With this video, we come to the end of this course on the XML standard, manipulating XML documents and processing them in server- and client-side applications. Hope you had a good time; we definitely did!

Video Transcript

00:01All right, so we reach one of the most exciting parts
00:04and actually our last major topic in this course
00:08before we start rehearsing
00:11and connecting all the dots together.
00:13All right, so our last major topic is talking about
00:16how could we modify the XML directly in PHP?
00:20Now, every single language that I know of
00:23is capable of editing XML,
00:25but I thought I'd keep that topic less
00:27and I would keep it to when we're working
00:29on the server side
00:30because that's really when it makes sense
00:32to actually modify XML on your own most of the time.
00:36But what we're gonna learn here
00:38is absolutely true to every other single language.
00:40You just have to figure out what the API to do it,
00:43which is really easy now after you've seen how to do it
00:46and how to work with XML
00:48in three completely separate languages
00:50that have three completely different styles of working.
00:54All right, so let's see.
00:54So first of all, I wanna just,
00:56just getting back to what we were doing before,
00:58I just wanna access one of our elements.
01:00Let's say I wanna access my first exam.
01:02So that was our XML student zero
01:08and I wanna access its first exam
01:11and if I just do your trace,
01:13we'll see that this value will come back as 80.
01:16Now, if I wanted to change the exam value,
01:19it's incredibly simple.
01:20All I have to do is just take this value,
01:23that node, set an equal sign
01:26and just say it equals 100.
01:29Now, by setting it equals 100 in line three,
01:31it's not saving it onto the hard drive,
01:33it's saving it virtually where we're located right now.
01:37And now, if I refresh my application,
01:39I'm gonna get an error message telling me
01:41that I cannot assign into an array.
01:45Absolutely true.
01:47What am I trying to do?
01:47I'm trying to put the value 100 into that array exam.
01:50So instead of that, what I wanna do
01:52is I wanna approach the first element in the exam
01:55and put that data into it.
01:56So let's try it now.
01:58There we go.
01:59So now that's fine.
02:00So if I, obviously we wanna approach our first element
02:03in the array also in our echo
02:05and that's still giving us our value.
02:07Now you might be thinking,
02:07well, Ben, you might've just deleted the whole thing.
02:10Maybe you deleted the type,
02:11maybe we're not getting really what we're expecting.
02:14So an easy way to check that is I could,
02:16instead of approach this whole element,
02:18I could say, okay, let's just print out the XML
02:21as it is at this stage.
02:22To do that, all I have to do is call this function
02:25on my XML as XML,
02:28all up our case for the words XML.
02:31If I refresh my screen, I should get the full XML.
02:33Now we can't really see it in Safari.
02:36So we're gonna have to right click view source
02:39and then let's just put it into the screen,
02:42make it nice and small.
02:44Here we go.
02:44So here's our XML.
02:46And we could see that in our XML,
02:48our value of Ben's math equals 100.
02:51Now, same deal, any other thing.
02:53So if I wanted to, for example,
02:54if I wanted to change the name of the first student,
02:57I would do the exact thing.
02:58I would approach the first student,
03:00then I would approach its attribute.
03:03We said that we access the attribute
03:05just by setting the attribute name.
03:06So let's say its name and we could just change the name
03:08to be Buffy.
03:12There we go, Buffy.
03:14Save that, refresh our application.
03:17Because we're working in Safari,
03:19I have to look at my source code.
03:20And here we go.
03:21The first student is actually called Buffy now
03:23and they're not called Ben.
03:25Now, obviously we didn't save it to the file,
03:27but imagine that we have a JavaScript,
03:30a Flash file, whatever it is,
03:31after we made the modifications we need to the XML
03:34and we output it to the stage,
03:36that's all we had to do.
03:37Now our applications that are leaning on this XML
03:40could get the most up-to-date information
03:42after our PHP did its modifications into it.
03:46So that was a really nice piece of work.
03:48Now, what would happen if I actually wanted
03:51to add more children?
03:52What if I wanted to add another student?
03:54How could I add another student?
03:56Let's see how to do it.
03:57So it's actually very, very nice and easy.
04:00All I have to do is this, follow me through.
04:03So first of all, I'm gonna approach my XML.
04:05I'm in my XML right now
04:07and I'm just gonna say I wanna add another child.
04:10Add another child is a function.
04:13So add child.
04:14The name of the child is going to be student
04:17to match with the other students that we've created.
04:20We wanna give a student a value.
04:22So if we want, we could set the value to be empty.
04:26We don't have to actually put a specific value inside.
04:29If I would put a value inside, see what would happen.
04:31So if I would put here a value, let's say, ben.
04:35And we would run our application.
04:38If I'd run my application,
04:39we would see first of all,
04:40we're getting ben here on the bottom.
04:41But if I look at my source code,
04:42I'm gonna see that I got here a simple student
04:45that has inside of a ben,
04:46which we don't really want that.
04:48So how about we just leave it empty?
04:50Let's see what happens
04:52if we leave it empty.
04:57And let's look at our source code.
04:59We could see the new student has been created
05:03and it's empty.
05:04Perfect, that's what we wanted.
05:05So first of all, we created an empty student.
05:08Now we wanna be able to reference that student.
05:10So I'm gonna create here a student variable
05:14and assign to the value that's coming back
05:16from this creation.
05:17So while it's creating that student,
05:19it's gonna return me a reference to that student.
05:22Now I could access the student
05:24and add to it a new attribute.
05:26To add in a new attribute,
05:28all I have to do is add attribute,
05:32set the attribute name, which is name,
05:34set the attribute value, which is going to be ben
05:36because we deleted our previous ben.
05:38And now our student has an attribute.
05:40If I save it, go back and refresh,
05:43look at our source code.
05:44I'm gonna see on the bottom here
05:45that I have a student and his name is ben.
05:48And in the same exact way,
05:50I could now add exams.
05:51So if I wanted to add exams to my student,
05:54I could approach my student directly
05:56because he's the one that I wanna add stuff to
05:58and I could add a child to it
06:00and I wanna add an exam to it.
06:03This exam will have a value.
06:04I got 50 in this first exam,
06:06but I do need to access that variable.
06:09So I'm gonna create here an exam variable
06:11so I could access that exam and modify its type.
06:17So I'll do that by adding an at reviewed.
06:20Now obviously it's starting to look weird
06:22even though it's not.
06:23So let's say this is going to be math
06:27and the type itself is going to be
06:30the name of the attribute.
06:31One thing that I would do, I like doing this.
06:33I would just put here an extra tab,
06:36put here an extras tab.
06:38I would even go as far as putting this into a tab
06:40because it would make it easier for me
06:42to see that they belong to each other.
06:44But this is really just about what you're comfortable with.
06:48So really what I'm doing right here
06:49and really what I'm doing here is I'm adding exams.
06:52And I could add three new exams.
06:54Let's say we do the math exam.
06:57Well let's do here a Spanish one.
07:01And in Spanish I got 80 let's say.
07:03And in let's say code,
07:09I got a 97.
07:12That is wicked of me.
07:14So if I refresh my application and run it again
07:16and it really doesn't matter if it doesn't look similar,
07:19what's really important here
07:21and if I just grab this code and just copy it,
07:24just close it up, the layout is really not important.
07:27It's about what the content that's inside.
07:30So if I just paste that element and just break it
07:32so we could see more easily that it's the exact same thing
07:36and we got exactly what we wanted
07:40and we'll break another line here
07:42and break another line here.
07:45Whoopsie.
07:46And I think you're ready to get it
07:47that we got here exactly what we expected.
07:50It was only condensed without all those break lines
07:52that we didn't really need.
07:53And there we go.
07:54So you learn now a lot of stuff
07:56in a very, very short amount of time.
07:57So I would watch this video maybe a couple of more times.
08:00Now what we learned right now is true for e4x.
08:03Very easy to access and change information in e4x.
08:07Not as easy with the XML dome but it's possible as well.
08:11Really easy to do with simple XML.
08:15Now in PHP there's no reason for me now
08:18to save it onto a file.
08:19All I could do is just echo it out.
08:21And now any application that needs to access
08:24this information could be referenced directly
08:26instead of being referenced directly to the XML,
08:29could be referenced to my dynamic XML
08:31that was built with my PHP.

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!