2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2010 */
6 /* Inclusive Design Institute */
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 /************************************************************************/
17 * Class for parsing XML result returned from transformable search
20 * @package tile_search Module
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 ResultParser() {
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_parser_set_option($this->parser,XML_OPTION_TARGET_ENCODING, "ISO-8859-1");
38 xml_set_element_handler($this->parser, 'startElement', 'endElement');
39 xml_set_character_data_handler($this->parser, 'characterData');
43 function parse($xml_data) {
44 $xml_data = trim($xml_data);
46 $this->element_path = array();
47 $this->result_rows = array();
48 $this->character_data = '';
50 xml_parse($this->parser, $xml_data, TRUE);
54 function startElement($parser, $name, $attributes)
56 array_push($this->element_path, $name);
60 /* called when an element ends */
61 /* removed the current element from the $path */
62 function endElement($parser, $name) {
63 // parse error message
64 if ($this->element_path == array('errors', 'totalCount'))
66 $this->result_rows['status'] = 'fail';
67 $this->result_rows['totalCount'] = trim($this->character_data);
69 else if ($this->element_path == array('errors', 'error', 'message'))
71 $this->result_rows['error'][] = trim($this->character_data);
74 // parse search results
75 else if ($this->element_path == array('resultset', 'summary', 'numOfTotalResults'))
77 $this->result_rows['status'] = 'success';
78 $this->result_rows['summary']['numOfTotalResults'] = trim($this->character_data);
80 else if ($this->element_path == array('resultset', 'summary', 'nmOfResults'))
82 $this->result_rows['summary']['nmOfResults'] = trim($this->character_data);
84 else if ($this->element_path == array('resultset', 'summary', 'lastResultNumber'))
86 $this->result_rows['summary']['lastResultNumber'] = trim($this->character_data);
88 else if ($this->element_path === array('resultset', 'results', 'result', 'courseID'))
90 $this->result_rows[$this->row_num]['courseID'] = trim($this->character_data);
92 else if ($this->element_path === array('resultset', 'results', 'result', 'title'))
94 $this->result_rows[$this->row_num]['title'] = trim($this->character_data);
96 // else if ($this->element_path === array('resultset', 'results', 'result', 'authorName'))
98 // $this->result_rows[$this->row_num]['authorName'] = trim($this->character_data);
100 else if ($this->element_path === array('resultset', 'results', 'result', 'creationDate'))
102 $this->result_rows[$this->row_num]['creationDate'] = trim($this->character_data);
104 else if ($this->element_path === array('resultset', 'results', 'result', 'description'))
106 $this->result_rows[$this->row_num]['description'] = trim($this->character_data);
108 // else if ($this->element_path === array('resultset', 'results', 'result', 'detailURL'))
110 // $this->result_rows[$this->row_num]['detailURL'] = trim($this->character_data);
112 // else if ($this->element_path === array('resultset', 'results', 'result', 'creativeCommons'))
114 // $this->result_rows[$this->row_num]['creativeCommons'] = trim($this->character_data);
116 else if ($this->element_path === array('resultset', 'results', 'result'))
121 array_pop($this->element_path);
122 $this->character_data = '';
126 function characterData($parser, $data){
127 $this->character_data .= $data;
131 function getNumOfResults()
133 return count($this->result_rows);
137 function getParsedArray()
139 return $this->result_rows;