Tải bản đầy đủ (.pdf) (5 trang)

PHP 5/MySQL Programming- P63 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (120.14 KB, 5 trang )

Simplifying the Menu Pages
The menu page isn’t as complicated when all the data is stored in the XML pages.
Each call to the system requires only one parameter: the name of the XML file
containing all the layout instructions. Here’s the menu page after changing it to
work with the XML files:
<h3>Main Menu</h3>
<!— menu page modified for XML version of CMS —>
<ul>
<li><a href = “XCMS.php?theXML=main.xml”>
main</a>
</li>
<li><a href = “XCMS.php?theXML=classes.xml”>
classes</a>
</li>
<li><a href = “XCMS.php?theXML=links.xml”>
links</a>
</li>
<li><a href = “XCMS.php?theXML=software.xml”>
software</a>
</li>
<li><a href = “XCMS.php?theXML=media.xml”>
media</a>
</li>
</ul>
This menu calls the XML version of the CMS code (XCMS.php) and sends to it the
XML filename that describes each page to be created. Of course, you must exam-
ine how the XML data is manipulated in that program. Start, though, with a sim-
pler program that looks at XML data.
Introducing XML Parsers
A program that reads and interprets XML data is usually called an
XML parser.


PHP 5 actually ships with three different XML parsers. I focus on the one that’s
easiest to use. It’s called the
simpleXML API and comes standard with PHP 5. An
API is an application programming interface—an extension that adds function-
ality to a language.
288
P
H
P
5
/M
y
S
Q
L
P
r
o
g
r
a
m
m
i
n
g
f
o
r
t

h
e
A
b
s
o
l
u
t
e
B
e
g
i
n
n
e
r
289
C
h
a
p
t
e
r
8
X
M
L

a
n
d
C
o
n
t
e
n
t
M
a
n
a
g
e
m
e
n
t
S
y
s
t
e
m
s
If you’re using another version of PHP, you can either try loading the simpleXML
API as an add-on or work with another XML parser. The DOM (Document Object
Model) parser, if it’s enabled, works much like simpleXML. Older versions of PHP

include a parser based on SAX (Simple API for XML). This is also relatively easy
to use, but uses a completely different model for file manipulation. Still, with care-
ful reading of the online Help, you can figure it out: The concepts remain the same.
If you can use simpleXML, it’s a great place to start, because it’s a very easy entry
into the world of XML programming.
Working with Simple XML
The simpleXML model is well named, because it’s remarkably simple to use once
you understand how it sees data. XML data can be thought of as a hierarchy tree
(much like the directory structure on your hard drive). Each element (except the
root) has exactly one parent, and each element has the capacity to have a num-
ber of children. The
simpleXML model treats the entire XML document as a special
object called an
XML node.
Table 8.1 illustrates the main methods of the
simplexml_element object.
These various elements manipulate an XML file to maneuver the various file
elements.
Working with the simpleXML API
Take a look at the XMLDemo program featured in Figure 8.8, which illustrates the
simpleXML API.
The HTML output isn’t remarkable, but the source code that generates the page
is interesting in a number of ways.
TRAP
Method Returns
->asXML() An XML string containing the contents of the node
->attributes() An associative array of the node’s attributes
->children() An array of simplexml_element nodes
->xpath() An array of simplexml_elements addressed by the path
TABLE 8.1 METHODS OF THE SIMPLEXML OBJECT

<!doctype html public “-//W3C//DTD HTML 4.0 //EN”>
<html>
<head>
<title>XML Demo</title>
</head>
<body>
<h1>XML Demo</h1>
<?
//load up main.xml and examine it
$xml = simplexml_load_file(“main.xml”);
print “<h3>original XML</h3> \n”;
$xmlText = $xml->asXML();
$xmlText = htmlentities($xmlText);
print “<pre>$xmlText</pre> \n”;
print “<h3>extract a named element</h3> \n”;
print $xml->title;
print “<br />”;
290
P
H
P
5
/M
y
S
Q
L
P
r
o

g
r
a
m
m
i
n
g
f
o
r
t
h
e
A
b
s
o
l
u
t
e
B
e
g
i
n
n
e
r

FIGURE 8.8
This program
analyzes an XML
data file from
my content
management
system.
print “<h3>Extract as an array</h3> \n”;
foreach ($xml->children() as $name => $value){
print “<b>$name:</b> $value<br /> \n”;
} // end foreach
?>
</body>
</html>
Creating a simpleXML Object
The first significant line of code uses the simplexml_load_file() command to
load an XML document into memory. This command loads a document and cre-
ates an instance of the
simpleXML object. All your other work with simpleXML
involves using the simpleXML object’s methods.
You can also create an XML object from a string using simplexml_load_string().
This might be useful if you want to build an XML file from within your code.
The XML object is stored in the aptly named $xml variable. I can then extract data
easily from XML.
Viewing the XML Code
It might be useful to look at the actual XML code as you explore the code, so I
reproduced it on the page.
simpleXML does not keep the data in its plain text for-
mat, but converts it into a special data structure so it is easier to use. If you do
want to see it as text-based XML, you can use the

asXML() method to produce the
XML code used to show part of the document. Note that you can use
asXML() on
the entire XML object or on specific subsets of it. This can be handy when you
need to debug XML code. XML code does not display well in an HTML page, so
I used PHP’s built-in
htmlentities() function to convert all HTML/XML characters
to their appropriate HTML entity tags, then displayed the entire XML document
inside a
<pre></pre> set.
Accessing XML Nodes Directly
If you know the names of various tags in your document, you can access elements
directly. For example, the following line pulls the
title element from the main page:
print $xml->title;
TRICK
291
C
h
a
p
t
e
r
8
X
M
L
a
n

d
C
o
n
t
e
n
t
M
a
n
a
g
e
m
e
n
t
S
y
s
t
e
m
s
Note that the top-level tag set in my document (<cpage></cpage>) is automatically
copied over to the
$xml variable. Since title is a cpage subtag, the value of title is
returned.
The node of an XML element can be seen as a string or an object. This can cause

confusion, because PHP won’t always treat a value extracted from XML exactly
like you expect.
The title element is actually not a string variable, but another simpleXML
object. You can use all the simpleXML methods on this object just as you do the
main one. In this case, I simply wanted to print the text associated with title.
simpleXML usually (but not always) correctly converts simpleXML elements
to strings. In some cases (particularly when you want to use the results of a
simpleXML query as part of an assignment or condition) you may need to force
PHP to treat the element as string data.
For example, the following condition does not work as expected:
if ($xml->title == “main”){
It won’t work because main is a string value and $xml->title is an object. They
may appear to human readers to have the same value, but since they have differ-
ent internal representations, PHP won’t always recognize them as the same thing
without minor coercion. You can use a technique called
type casting
to resolve
this problem.
if ((string)$xml->title == “main”){
This version of the code forces the value from $xml->title into a string repre-
sentation so it can be compared correctly.
Using a foreach Loop on a Node
Much of the time you work with XML through various looping structures. Since
XML code consists of name-value structures, it won’t surprise you to find asso-
ciative arrays especially helpful. The following code steps through a simple XML
file and extracts the name and value of every tag evident from the top layer.
print “<h3>Extract as an array</h3> \n”;
foreach ($xml->children() as $name => $value){
print “<b>$name:</b> $value<br /> \n”;
} // end foreach

The reference to $xml->children() is a call to the $xml simpleXML object’s children()
method. This method returns an array of all the nodes belonging to $xml. Each of
the elements in the array is a new
simpleXML object with all the same methods
as
$xml. Since the children() method returns an array of values, I can use the
foreach loop to conveniently step through each element of the array. Using
TRAP
292
P
H
P
5
/M
y
S
Q
L
P
r
o
g
r
a
m
m
i
n
g
f

o
r
t
h
e
A
b
s
o
l
u
t
e
B
e
g
i
n
n
e
r

×