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

Elements and Attributes in PHP

How to access elements and attribute values in PHP

We saw in the previous summary. that while we can directly output XML document data that is simply organized as a root element with content (and no nested elements within,) we aren't lucky enough to get such simply structured documents in everyday software tasks or projects. PHP language implementations provides syntactic constructs to work around the problem we saw previously; namely that elements with other nested elements draw a total blank when an attempt is made to render them.

There are two main constructs provided:

  1. Accessing content in nested elements using the '→' operator
  2. Accessing attribute values using the '[]' indexing operator.

Probably a live example would clarify the above statements. Consider an XML document that has the following information stored: a description of the chassis of an automobile. For example:

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

<automobile id="786">

<model>Volkswagen Jetta</model>

<chassis>

<battery model="amaron" power="500W"/>

<radiator coolant="ethanol"/>

<engine model="VW1200" capacity="1200cc" hp="100"/>

</chassis>

</automobile>

The PHP code sample given below explains how to extract all the information regarding the model name and engine specification of a car manufactured by a German automobile giant. Please read carefully... the syntax is very simple and intuitive... just try to visualize a tree structure:

<?

$xml = simple_xml_load_file("auto.xml");

echo $xml->automobile->model;

echo $xml->automobile->chassis->engine[model];

?>

The output will be the value of the model element and the attribute value of model inside the engine element. A complication arises if the tag name of an XML element has the character '-' as part of the label. For example, if the attribute "model" of the element "automobile" were to be renamed as "engine-model", then the statement to extract the model attribute would be changed. The name should be enclosed in double quotes, as in "engine-model"; unlike the other scenario, where we could use the attribute name un-quoted.

So we can now handle both trivial as well as non-trivial XML document formats in PHP. Handling means loading, opening, parsing and processing. There are other useful APIs for handling XML documents in the language, which we'll encounter in the following videos.

Video Transcript

00:00All right, so let's clean up and move on to the next step,
00:02which is actually retrieving that data
00:05and seeing how we could actually access it.
00:07So let's take a first, let's take a look
00:09at how to access nodes or nodes.
00:14Well, that's really what we wanna access.
00:16Okay, so let's see how we do that.
00:18So first of all, it's incredibly simple.
00:21So first of all, if we're in the,
00:23looking at this XML that we loaded,
00:25if we even just look right now, we're in here.
00:28To access the header, all I have to do
00:30is just enter into the header.
00:32This is the way I do it.
00:35Done, now I'm in the header.
00:37Now, if I wanna go deeper in, I wanna go into the title,
00:40all I have to do is just title and there we go.
00:44And then I have direct access.
00:46I could save this and if we echo this out and test it out,
00:49well, you should see the content of that XML node.
00:54Now, if our node had a name that was illegal to PHP,
00:58such as minus is not allowed
01:00because it's part of the language,
01:01this is how we access minus and greater than,
01:04this is how we access our nodes.
01:07So what would happen if our title wasn't called title,
01:10but it was called, it had a different name.
01:12Let's say it was called my title.
01:17What would happen if our XML, from an XML perspective,
01:19this is legal, but from a PHP perspective,
01:22this is absolutely not legal.
01:24I could definitely not come here and type my title.
01:27If I would do that and I would have your PHP
01:30actually generating errors,
01:31we would see here that we're doing here
01:33something completely unexpected and unappreciated by PHP.
01:39So let's see how we could solve this.
01:40It's actually quite simple the way we solve it.
01:42We have to open up a curly bracket
01:44and close a curly bracket
01:46and then just paste inside this as a string instead.
01:49So basically it's the exact same structure as we had before,
01:52only instead of that,
01:53we're putting it in between two curly brackets.
01:56It's very similar to the way how in PHP,
01:58how in JavaScript and Flash,
02:00you would actually use an object and then put it in size.
02:03You'd use the bracket,
02:06but in our scenario, we're using a curly bracket
02:08instead of the squared bracket,
02:10which is fair enough, close enough.
02:12And in Flash, we might not have this type of syntax,
02:16but it's close enough and easy enough to understand.
02:18So not too bad, not that hard to get used to.
02:22So let's just test this out
02:23and make sure that we're actually getting our information
02:26and we're not getting our information.
02:28So let's see,
02:29because obviously we're doing here something wrong.
02:31We're not having any errors in our script,
02:34but we're not accessing the header
02:36while we were supposed to access the header.
02:38So it actually, it should have been,
02:40instead of all this, there was no title in the end
02:43and it actually was header and that should work.
02:48Because we had to put first the header and then my title
02:51and you can see the position of it really does not matter.
02:55And there we go, and we got our title back again.
02:59All right, last basic topic of accessing information
03:02is really how do we access attributes?
03:05Accessing attributes is even easier.
03:08It's actually really, really easy.
03:10So the way we access attributes,
03:12for example, if we're in the header,
03:14which we are in the header right here,
03:17and we wanted to access that attribute, which is called ATT,
03:21all right, so what we have to do is now,
03:23and we know we want to get to the attribute ATT.
03:26So all I have to do is open a squared bracket type ATT.
03:31So really the biggest difference here
03:33is that I'm not accessing with that minus N smaller
03:38than instead of that I'm accessing it
03:40as a element in the C instead as PHP calls it.
03:46So if I refresh my screen now,
03:48I could see my sample attribute.
03:49Now, please notice that a lot of programming languages
03:53would have dot ATT.
03:55Dot ATT and PHP is absolutely wrong.
03:57What dot actually means in PHP
03:59is plus for two different strings.
04:01So in this scenario, this would equal nothing,
04:04and this would probably equal ATT
04:06if it wouldn't generate an error.
04:08And if we would run this this way,
04:09we would probably get a value similar to ATT,
04:12and it's absolutely not what we want.
04:14What we actually want is to access the information
04:16and to access the information in PHP,
04:18we have two options of doing that.
04:20One is entering the indices,
04:22and the other one is using that minus N smaller than,
04:27which would go inside of its properties.
04:30All right, so that really covers
04:33the access to both nodes, elements,
04:37well, nodes that are elements and attributes,
04:39and that covers really the basis of this section.

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!