Here’s the menu page after changing it to work with the XML files: Main Menu main classes links software media This menu calls the XML version of the CMS code XCMS.php and s
Trang 1Simplifying 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 simpleXMLAPI and comes standard with PHP 5 An API is an application programming interface—an extension that adds function-ality to a language
288
g r
s o
l u
g in
e r
Trang 2If 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 simpleXMLmodel 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 simpleXMLmodel treats the entire XML document as a special
object called an XML node Table 8.1 illustrates the main methods of the
simplexml_elementobject
These various elements manipulate an XML file to maneuver the various file
elements
Working with the simpleXML API
Take a look at the XMLDemoprogram featured in Figure 8.8, which illustrates the
simpleXMLAPI
The HTML output isn’t remarkable, but the source code that generates the page
is interesting in a number of ways
T R A P
->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
T A B L E 8 1 M E T H O D S O F T H E S I M P L E X M L O B J E C T
Trang 3<!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
g r
s o
l u
g in
e r
FIGURE 8.8
This program
analyzes an XML
data file from
my content
management
system.
Trang 4print “<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 simpleXMLobject’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 $xmlvariable 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 simpleXMLdoes 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 titleelement from the main page:
print $xml->title;
T R I C K
291
Trang 5Note that the top-level tag set in my document (<cpage></cpage>) is automatically copied over to the $xmlvariable Since titleis a cpagesubtag, 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 represdiffer-entations, 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 simpleXMLobject’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 simpleXMLobject 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
T R A P 292
g r
s o
l u
g in
e r