<< Page Travaux

Extracting data from an XML document using PHP and SimpleXML

Conceptual goals

For this exercise I modified an existing XML file containing data on lessons within online course content for ALPECOLe (a SVC project at the University of Zürich for which I am working). The course includes mandatory and optional content. I wished to create an easy reference table that would allow project participants and authors to have a quick overview of each lesson's parts. PHP and SimpleXML were used to extract the desired data.

Technical goals

Process

Design:

The actual ALPECOLe course contains 30 lessons. For the purpose of this exercise I had to modify (add data) to the XML file, so I reduced it to 6 lessons. The result of the extracted data was a table that listed each lesson and page within (uri), with a link to that page (relative link that is not working in this instance) and the type of content found on that page, whether mandatory, optional, or one of the standard pages common to all lessons (start page, learning objectives and a review test).

The resulting page would have been quite long and difficult to scroll through if one were looking for information on a particular lesson, so to make the results more ergonomic, the initial page posts just some overall statistics on the type of content within each lesson, with the option to view the details on each particular lesson. Both the initial overview page and the page that posts the selected details provide the option to view details for all lessons.

For the overall look I adapted the style sheet I have been using for my assignments and adopted the colour scheme from the ALPECOLe course to allow the users to recognize the interface as part of the course development environment.

Technical:

As a starting point I had an XML flle from a document that is used within a javaservlet to create a graphic navigation tree based on exported imagemap attributes. The collected data is set entirely within attributes. This caused me great difficulties because of my unfamiliarity with the proper SimpleXML and Php syntax for referencing attributes. With help from forum postings and the PHP manual I was able to get all the desired data to post and create some variables that were manipulated to show some basic statistics as to the type of content of each page (uri) within a lesson.

Using the foreach() function applied to the first element (after the root element – thanks to Natascha's forum answer for that), I was able display all the lessons and to then create a looping foreach() function (actually a 'control structure' according to the Php manual) within it to display the data for each page (<node>) within each lesson.

Once I had all the desired data for all the lessons displaying, I chose to display only the overall statistics on the initial entry page. This was easily done by simply removing the php output functions (printr, echo) that were used to display the details. I then added a form for each lesson to send the selected lesson (retrieved through the $lessonNum variable that took the value of the lesson attribute within the first child element ($tree['lesson']). I used a drop menu with one selected option and a submit button. From a usability perspective this is not optimal, so I inserted style attribute with the display parameter set to none for the actual menu, leaving only the submit button visible.

Using an if statement that matched the retrieved variable (from the submitted lesson choice), to the lesson attribute of the of the lesson within the XML document that I wished to display, I was able to display the detailed data for a single lesson selected by the user.

Documents

welcome.html, extract_table.php (source), details.php (source) extract_table_all.php (source), lessons.xml, css

References

February 13 , 2006 k.b.

valid XHTML, valid CSS