2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto */
9 /* This program is free software. You can redistribute it and/or */
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation. */
12 /************************************************************************/
13 // $Id: MerlotResultParser.class.php 7208 2008-02-08 16:07:24Z cindy $
17 * Class for parsing XML result returned from merlot search
20 * @package Merlot Module
22 class MerlotResultParser {
25 var $parser; // the XML handler
26 var $result_rows = array(); // the module data
27 var $character_data; // tmp variable for storing the data
28 var $element_path; // array of element paths (basically a stack)
32 function MerlotResultParser() {
33 $this->parser = xml_parser_create('');
35 xml_set_object($this->parser, $this);
36 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false); /* conform to W3C specs */
37 xml_set_element_handler($this->parser, 'startElement', 'endElement');
38 xml_set_character_data_handler($this->parser, 'characterData');
42 function parse($xml_data) {
43 $this->element_path = array();
44 $this->result_rows = array();
45 $this->character_data = '';
47 xml_parse($this->parser, $xml_data, TRUE);
51 function startElement($parser, $name, $attributes)
53 array_push($this->element_path, $name);
57 /* called when an element ends */
58 /* removed the current element from the $path */
59 function endElement($parser, $name) {
60 if ($this->element_path == array('merlotMaterialSearchWebService', 'status'))
62 $this->result_rows['status'] = trim($this->character_data);
64 else if ($this->element_path == array('merlotMaterialSearchWebService', 'error', 'message'))
66 $this->result_rows['error'] = trim($this->character_data);
68 else if ($this->element_path == array('merlotMaterialSearchWebService', 'summary', 'totalCount'))
70 $this->result_rows['summary']['totalCount'] = trim($this->character_data);
72 else if ($this->element_path == array('merlotMaterialSearchWebService', 'summary', 'resultCount'))
74 $this->result_rows['summary']['resultCount'] = trim($this->character_data);
76 else if ($this->element_path == array('merlotMaterialSearchWebService', 'summary', 'lastRecNumber'))
78 $this->result_rows['summary']['lastRecNumber'] = trim($this->character_data);
80 else if ($this->element_path === array('merlotMaterialSearchWebService', 'results', 'material', 'title'))
82 $this->result_rows[$this->row_num]['title'] = trim($this->character_data);
84 else if ($this->element_path === array('merlotMaterialSearchWebService', 'results', 'material', 'URL'))
86 $this->result_rows[$this->row_num]['URL'] = trim($this->character_data);
88 else if ($this->element_path === array('merlotMaterialSearchWebService', 'results', 'material', 'authorName'))
90 $this->result_rows[$this->row_num]['authorName'] = trim($this->character_data);
92 else if ($this->element_path === array('merlotMaterialSearchWebService', 'results', 'material', 'creationDate'))
94 $this->result_rows[$this->row_num]['creationDate'] = trim($this->character_data);
96 else if ($this->element_path === array('merlotMaterialSearchWebService', 'results', 'material', 'description'))
98 $this->result_rows[$this->row_num]['description'] = trim($this->character_data);
100 else if ($this->element_path === array('merlotMaterialSearchWebService', 'results', 'material', 'detailURL'))
102 $this->result_rows[$this->row_num]['detailURL'] = trim($this->character_data);
104 else if ($this->element_path === array('merlotMaterialSearchWebService', 'results', 'material', 'creativeCommons'))
106 $this->result_rows[$this->row_num]['creativeCommons'] = trim($this->character_data);
108 else if ($this->element_path === array('merlotMaterialSearchWebService', 'results', 'material'))
113 array_pop($this->element_path);
114 $this->character_data = '';
118 function characterData($parser, $data){
119 $this->character_data .= $data;
123 function getNumOfResults()
125 return count($this->result_rows);
129 function getParsedArray()
131 return $this->result_rows;