forked from ss23/php-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxml.html
369 lines (350 loc) · 15.2 KB
/
xml.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
---
title: Sinfully simple
---
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>PHP101 - {{ page.title }}</title>
<link rel="stylesheet" href="stylesheets/styles.css">
<link rel="stylesheet" href="stylesheets/pygment_trac.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="javascripts/main.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
</head>
<body>
<header>
<h1>PHP101</h1>
<p>For the absolute beginner</p>
</header>
<div id="banner">
<span id="logo"></span>
<a href="https://github.com/ss23/php-tutorial" class="button fork"><strong>View On GitHub</strong></a>
</div><!-- end banner -->
<div class="wrapper">
{% include nav.html %}
<section>
<h2>Easy peasy</h2>
<p>Unless you’ve been hiding in a cave for the last few years, you’ve heard about XML – it’s
the toolkit that more and more Web publishers are switching to for content markup. You may
even have seen an XML document in action, complete with user-defined tags and markup, and
you might have wondered how on earth one converts that tangled mess of code into
human-readable content.</p>
<p>The answer is, not easily.</p>
<p>While PHP has included support for the two standard methods of parsing (read: making sense
of) XML – SAX and DOM – since version 4.0, the complexity and inherent geekiness of these
methods often turned off all but the most dedicated XML developers. All that has changed,
however, with PHP 5.0, which introduces a brand-spanking-new XML extension named SimpleXML
that takes all (and I do mean all) the pain out of processing XML documents. Keep reading,
and find out how.</p>
<h2>The bad old days</h2>
<p>In order to understand why SimpleXML is so cool, a brief history lesson is in order.</p>
<p>In the days before SimpleXML, there were two ways of processing XML documents. The first,
SAX or the Simple API for XML, involved traversing an XML document and calling
specific functions as the parser encountered different types of tags. For example, you might
have called one function to process a starting tag, another function to process an ending tag,
and a third function to process the data between them. The second, DOM or the Document
Object Model, involved creating a tree representation of the XML document in memory, and then
using tree-traversal methods to navigate it. Once a particular node of the tree was reached,
the corresponding content could be retrieved and used.</p>
<p>Neither of these two approaches was particularly user-friendly: SAX required the developer
to custom-craft event handlers for each type of element encountered in an XML file, while
the DOM approach used an object-oriented paradigm which tended to throw developers off, in
addition to being memory-intensive and thus inefficient with large XML documents. In the
larger context also, PHP 4 used a number of different backend libraries for each of its
different XML extensions, leading to inconsistency in the way different XML extensions
worked and thus creating interoperability concerns (as well as a fair amount of confusion
for developers).</p>
<p>With PHP 5.0, a concerted effort was made to fix this problem, by adopting the libxml2
library (<a href="http://www.xmlsoft.org">http://www.xmlsoft.org</a>)
as the standard library for all XML extensions and by getting the various XML extensions
to operate more consistently. The biggest change in the PHP 5 XML pantheon, though, is
the SimpleXML extension developed by Sterling Hughes, Rob Richards and Marcus Börger,
which attempts to make parsing XML documents significantly more user-friendly than it
was in PHP 4.</p>
<p>SimpleXML works by converting an XML document into an object, and then turning the
elements within that document into object properties which can be accessed using standard
object notation. This makes it easy to drill down to an element at any level of the XML
hierarchy to access its content. Repeated elements at the same level of the document tree
are represented as arrays, while custom element collections can be created using XPath
location paths (of which, more later); these collections can then be processed using PHP’s
standard loop constructs. Accessing element attributes is as simple as accessing the keys
of an associative array – there’s nothing new to learn, and no special code to write.</p>
<p>In order to use SimpleXML and PHP together, your PHP build must include support for
SimpleXML. This support is enabled by default in both the UNIX and Windows versions of
PHP 5. Read more about this at <a href="http://www.php.net/manual/en/ref.simplexml.php">http://www.php.net/manual/en/ref.simplexml.php</a>. If you’re a PHP 4
user, you’re out of luck – SimpleXML is only available for PHP 5.</a>
<h2>Petting zoo</h2>
<p>To see how SimpleXML works, consider the following XML file:</p>
{% highlight xml %}<?xml version="1.0"?>
<pet>
<name>Polly Parrot</name>
<age>3</age>
<species>parrot</species>
<parents>
<mother>Pia Parrot</mother>
<father>Peter Parrot</father>
</parents>
</pet>{% endhighlight %}
<p>Now, you need a way to get to the content enclosed between the <name>,
<age>, <species> and <parents> elements. With SimpleXML, it’s a snap:</p>
{% highlight php %}<?php
// set name of XML file
$file = "pet.xml";
// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
// access XML data
echo "Name: " . $xml->name . "\n";
echo "Age: " . $xml->age . "\n";
echo "Species: " . $xml->species . "\n";
echo "Parents: " . $xml->parents->mother . " and " . $xml->parents->father . "\n";
?>{% endhighlight %}
<p>The action begins with the simplexml_load_file() function, which accepts
the path and name of the XML file to be parsed. The result of parsing the file is a
PHP object, whose properties correspond to the elements under the root element. The
character data within an element can then be accessed using standard
object->property notation, beginning with the root element and moving
down the hierarchical path of the document.</p>
<p>Just as you can read, so also can you write. SimpleXML makes it easy to alter the
contents of a particular XML element – simply assign a new value to the corresponding
object property. Here’s an example:</p>
{% highlight php %}<?php
// set name of XML file
$file = "pet.xml";
// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
// modify XML data
$xml->name = "Sammy Snail";
$xml->age = 4;
$xml->species = "snail";
$xml->parents->mother = "Sue Snail";
$xml->parents->father = "Sid Snail";
// write new data to file
file_put_contents($file, $xml->asXML());
?>{% endhighlight %}
<p>Here, the original XML file is first read in, and then the character data enclosed within
each element is altered by assigning new values to the corresponding object property. The
asXML() method, typically used to dump the XML tree back out to the standard
output device, is in this instance combined with the file_put_contents()
function to overwrite the original XML document with the new data.</p>
<h2>Sin city</h2>
<p>Repeated elements at the same level of the XML hierarchy are represented as array elements,
and can be accessed using numeric indices. To see how this works, consider the following XML file:</p>
{% highlight xml %}<?xml version="1.0"?>
<sins>
<sin>pride</sin>
<sin>envy</sin>
<sin>anger</sin>
<sin>greed</sin>
<sin>sloth</sin>
<sin>gluttony</sin>
<sin>lust</sin>
</sins>{% endhighlight %}
<p>Here’s the PHP script that reads it and retrieves the data from it:</p>
{% highlight php %}<?php
// set name of XML file
$file = "sins.xml";
// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
// access each <sin>
echo $xml->sin[0] . "\n";
echo $xml->sin[1] . "\n";
echo $xml->sin[2] . "\n";
echo $xml->sin[3] . "\n";
echo $xml->sin[4] . "\n";
echo $xml->sin[5] . "\n";
echo $xml->sin[6] . "\n";
?>{% endhighlight %}
<p>If you’d prefer, you can even iterate over the collection with a foreach()
loop, as in this next, equivalent listing:</p>
{% highlight php %}<?php
// set name of XML file
$file = "sins.xml";
// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
// iterate over <sin> element collection
foreach ($xml->sin as $sin) {
echo "$sin\n";
}
?>{% endhighlight %}
<h2>The shape of things to come</h2>
<p>SimpleXML handles element attributes as transparently as it does elements and their
content. Attribute-value pairs are represented as members of a PHP associative array,
and can be accessed like regular array elements. To see how this works, take a look
at this script:</p>
{% highlight php %}<?php
<?php
// create XML string
$str = <<< XML
<?xml version="1.0"?>
<shapes>
<shape type="circle" radius="2" />
<shape type="rectangle" length="5" width="2" />
<shape type="square" length="7" />
</shapes>
XML;
// load string
$xml = simplexml_load_string($str) or die ("Unable to load XML string!");
// for each shape
// calculate area
foreach ($xml->shape as $shape) {
if ($shape['type'] == "circle") {
$area = pi() * $shape['radius'] * $shape['radius'];
} else if ($shape['type'] == "rectangle") {
$area = $shape['length'] * $shape['width'];
} elseif ($shape['type'] == "square") {
$area = $shape['length'] * $shape['length'];
}
echo $area."\n";
}
?>{% endhighlight %}
<p>Unlike previous examples, which used an external XML file, this one creates the XML
dynamically and loads it into SimpleXML with the simplexml_load_string()
method. The XML is then parsed with a foreach() loop, and the area for
each shape calculated on the basis of the value of each <shape>
element’s type attribute. The listing above demonstrates how attribute values can
be accessed as keys of the attribute array associated with each element property.</p>
<h2>X marks the spot</h2>
<p>SimpleXML also supports custom element collections, through XPath location paths. For
those of you new to XML, XPath is a standard addressing mechanism for an XML
document, allowing developers to access collections of elements, attributes or text
nodes within a document. Read more about XPath at
<a href="http://www.w3.org/TR/xpath.html">http://www.w3.org/TR/xpath.html</a> and
<a href="http://www.melonfire.com/community/columns/trog/article.php?id=83">http://www.melonfire.com/community/columns/trog/article.php?id=83</a>.</p>
<p>To see how this works, consider the following XML document:</p>
{% highlight xml %}<?xml version="1.0"?>
<ingredients>
<item>
<desc>Boneless chicken breasts</desc>
<quantity>2</quantity>
</item>
<item>
<desc>Chopped onions</desc>
<quantity>2</quantity>
</item>
<item>
<desc>Ginger</desc>
<quantity>1</quantity>
</item>
<item>
<desc>Garlic</desc>
<quantity>1</quantity>
</item>
<item>
<desc>Red chili powder</desc>
<quantity>1</quantity>
</item>
<item>
<desc>Coriander seeds</desc>
<quantity>1</quantity>
</item>
<item>
<desc>Lime juice</desc>
<quantity>2</quantity>
</item>
</ingredients>{% endhighlight %}
<p>Now, let’s suppose you want to print all the <desc> elements. You
could do it by iterating over the array of <item> elements, as
discussed earlier…or you could just create a custom collection of only the
<desc> elements with the xpath() method, and
iterate over that instead:</p>
{% highlight php %}<?php
// set name of XML file
$file = "ingredients.xml";
// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
// get all the <desc> elements and print
foreach ($xml->xpath('//desc') as $desc) {
echo "$desc\n";
}
?>{% endhighlight %}
<p>Using XPath, you can get even fancier than this – for example, by creating a collection
of only those <desc> elements whose corresponding quantities are two or more.</p>
{% highlight php %}<?php
// set name of XML file
$file = "ingredients.xml";
// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
// get all the <desc> elements and print
foreach ($xml->xpath('//item[quantity > 1]/desc') as $desc) {
echo "$desc\n";
}
?>{% endhighlight %}
<p>Without XPath, accomplishing this would be far more complicated than the five lines of code
above…try it for yourself and see!</p>
<h2>An evening at the Moulin Rouge</h2>
<p>Now that you’ve seen what XPath can do, let’s wrap this up with an example of how you
might actually use it. Let’s suppose you have a bunch of movie reviews marked up in XML,
like this:</p>
{% highlight xml %}<?xml version="1.0"?>
<review id="57" category="2">
<title>Moulin Rouge</title>
<teaser>
Baz Luhrmann's over-the-top vision of Paris at the turn of the century is witty, sexy...and completely unforgettable
</teaser>
<cast>
<person>Nicole Kidman</person>
<person>Ewan McGregor</person>
<person>John Leguizamo</person>
<person>Jim Broadbent</person>
<person>Richard Roxburgh</person>
</cast>
<director>Baz Luhrmann</director>
<duration>120</duration>
<genre>Romance/Comedy</genre>
<year>2001</year>
<body>
A stylishly spectacular extravaganza, Moulin Rouge is hard to
categorize; it is, at different times, a love story, a costume drama,
a musical, and a comedy. Director Baz Luhrmann (well-known for the
very hip William Shakespeare's Romeo + Juliet) has taken some simple
themes - love, jealousy and obsession - and done something completely
new and different with them by setting them to music.
</body>
<rating>5</rating>
</review>{% endhighlight %}
<p>Now, you want to display this review on your Web site. So, you need a PHP script to
extract the data from this file and place it in the appropriate locations in an HTML
template. With everything you’ve learned so far, this is a snap…as the code below
illustrates:</p>
{% highlight php %}<?php
// set name of XML file
// normally this would come through GET
// it's hard-wired here for simplicity
$file = "57.xml";
// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
?>
<html>
<head></head>
<body>
<!-- title and year -->
<h1><?php echo $xml->title; ?> (<?php echo $xml->year; ?>)</h1>
<!-- slug -->
<h3><?php echo $xml->teaser; ?></h3>
<!-- review body -->
<?php echo $xml->body; ?>
<!-- director, cast, duration and rating -->
<p align="right"/>
Director: <b><?php echo $xml->director; ?></b>
Duration: <b><?php echo $xml->duration; ?> min</b>
Cast: <b><?php foreach ($xml->cast->person as $person) { echo "$person "; } ?></b>
Rating: <b><?php echo $xml->rating; ?></b>
</body>
</html>{% endhighlight %}
<p>Pretty simple, huh?</p>
<p>That’s about all for the moment. In <a href="part12.html">Part Twelve</a> of PHP 101, I’ll be telling you all
about the new exception handling model in PHP 5, showing you how you can use it to
catch your scripts before they crash and burn. See you there!</p>
</section>
<footer>
<p><small>Hosted on GitHub Pages — Theme by <a href="http://twitter.com/#!/michigangraham">mattgraham</a></small></p>
</footer>
</div>
<!--[if !IE]><script>fixScale(document);</script><!--<![endif]-->
</body>
</html>