This allows me to rapidly print all the data in the XML element according to whatever format I wish.. Manipulating More-Complex XML with the simpleXML API The features demonstrated in th
Trang 1the foreachloop’s associative variant (described in chapter 5, “Better Arrays and
String Handling”) allows access to the document’s name/value pairs
Each time through the loop, the current tag is stored in $nameand its associated
value is stored in $value This allows me to rapidly print all the data in the XML
element according to whatever format I wish
Manipulating More-Complex XML
with the simpleXML API
The features demonstrated in the XMLdemo are enough for working with the
extremely simple XML variant used in the XCMS system, but you will want to
work with more-complex XML files with multiple tags As an example, consider
the following code, which could be used in an XML-enabled form of the quiz
pro-gram featured in chapter 6, “Working with Files.”
<?xml version=”1.0” encoding=”utf-8”?>
<test>
<problem type=”mc”>
<question>What is your name?</question>
<answerA>Roger the Shrubber</answerA>
<answerB>Galahad the pure</answerB>
<answerC>Arthur, King of the Britons</answerC>
<answerD>Brave Sir Robin</answerD>
<correct>C</correct>
</problem>
<problem type=”mc”>
<question>What is your quest?</question>
<answerA>I seek the holy grail</answerA>
<answerB>I’m looking for a swallow</answerB>
<answerC>I’m pining for the Fjords</answerC>
<answerD>I want to be a lumberjack!</answerD>
<correct>A</correct>
</problem>
<problem type=”mc”>
<question>What is your favorite color?</question>
<answerA>Red</answerA>
<answerB>Green</answerB>
<answerC>Orange</answerC>
<answerD>Yellow No, Blue!</answerD>
<correct>D</correct>
293
Trang 2<problem type=”mc”>
<question>What is your command?</question>
<answerA>I’m not to leave the room until you come and get him</answerA>
<answerB>I’m going with you</answerB>
<answerC>I’m not to let him enter the room</answerC>
<answerD>It seems daft to be guarding a guard!</answerD>
<correct>A</correct>
</problem>
</test>
This code is a little more typical of most XML data because it has multiple levels
of encoding The entire document can be seen as an array of problem nodes, and each problem node can be viewed as a set of answers and the correct answer The simpleXMLAPI can handle these more-complex documents with ease, as shown in Figure 8.9
When the XML code is a little more complex, you may need to carefully examine the raw XML code to best interpret it Once I recognized that the document is essentially an array of problems, the XML interpretation became relatively easy:
<!doctype html public “-//W3C//DTD HTML 4.0 //EN”>
<html>
<head>
294
l u
FIGURE 8.9
The quizreader.php
program reads an
XML file and
formats it in HTML.
Trang 3<title>Quiz Reader</title>
</head>
<body>
<?
//quiz reader
//demonstrates working with more complex XML files
//load up a quiz file
$xml = simplexml_load_file(“python.xml”);
//step through quiz as associative array
foreach ($xml->children() as $problem){
//print each question as an ordered list.
print <<<HERE
<h3>$problem->question</h3>
<ol type = “A”>
<li>$problem->answerA</li>
<li>$problem->answerB</li>
<li>$problem->answerC</li>
<li>$problem->answerD</li>
</ol>
HERE;
} // end foreach
//directly accessing a node:
print $xml->problem[0]->question;
?>
</body>
</html>
This procedure can be done in a number of steps:
1 Load the quiz as XML data
2 Use a foreachloop to examine each element of the xml’s children()array
as an individual problem In this example, the $problemvariable doesn’t
contain simple string data, but another node with its own elements
3 Inside the loop, use tag references to indicate the elements of the problem
you wish to display (Note that I chose not to display each question’s answer,
but I could have if I wished.)
295
Trang 4l u
If you want to display a particular element’s value, do so using array-style syntax This line refers to problem number 0:
print $xml->problem[0]->question;
It then looks for a subelement of a problem called question and displays that value If you are working with an extremely complicated XML document, you can use structures like this to map directly to a particular element Essentially, the simpleXMLsystem lets you think of an XML document as a set of nested arrays This allows you access to a potentially complex document in whatever detail you wish
Returning to XCMS
With all this XML knowledge, you’re ready to refit the CMS introduced earlier in this chapter with an XML structure My basic plan for the XCMS is to allow more parameters for each page The original CMS (without XML) allows two parameters The parameters are added directly to URLs with the post method This quickly becomes unwieldy By switching to an XML format, I can place all the parameters
I N THE R EAL W ORLD
You might want to use XML data in these main instances:
• You want a better organizational scheme for your information but don’t want
to deal with a formal database system In this case you can create your own XML language and build programs that work with the data This is the use
of XML described in this chapter
• You have XML data formatted in a predefined XML structure that you want
to manipulate There are XML standards published for describing everything from virtual reality scenes and molecular models to multimedia slideshows You can use the features of simpleXML (or one of PHP’s other XML parsers) to manipulate this existing data and create your own program for interpreting
it Of course, HTML is rapidly becoming a subset of XML (in fact, the XHTML standard is simply HTML following stricter XML standards), so you can use XML tricks to load and manage an HTML file This might be useful for extract-ing a Web page’s links or examinextract-ing a Web page’s images
• You need a stand-in for relational data Most database management systems allow you import and export data in XML format XML can be a terrific way
to send complex data, such as database query results or complete data tables, to remote programs Often programmers write client-side code in a language such as JavaScript or Flash and use a server-side program to send query results to the client as XML data
Trang 5necessary for displaying a page into an XML document, then have my CMS
pro-gram extract that data from the document
Extracting Data from the XML File
The XCMS program relies on repeated calls to the same program to generate the page
data The XCMS program is much like simpleCMSexcept—rather than pulling
para-meters directly from the URL—XCMS takes an XML filename as its single parameter
and extracts all the necessary information from that file
<?
//XCMS
//XML-Based Simple CMS system
//Andy Harris for PHP/MySQL Adv Beg 2nd Ed.
// NOTE: Requires simpleXML extensions in PHP 5.0!
//get an XML file or load a default
if (empty($theXML)){
$theXML = “main.xml”;
} // end if
//Open up XML file
$xml = simplexml_load_file($theXML);
if ( !$xml){
print (“there was a problem opening the XML”);
} else {
include ($xml->css);
include($xml->top);
print “<span class = \”menuPanel\”> \n”;
include ($xml->menu);
print “</span> \n”;
print “<span class = \”item\”> \n”;
include ($xml->content);
print “</span> \n”;
} // end if
?>
297