|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 /* 00003 * $Id: BasicXmlParser.php,v 1.2 2010/12/14 17:36:00 moodlerobot Exp $ 00004 * 00005 * Copyright(c) 2004-2006, SpikeSource Inc. All Rights Reserved. 00006 * Licensed under the Open Software License version 2.1 00007 * (See http://www.spikesource.com/license.html) 00008 */ 00009 ?> 00010 <?php 00011 00012 require_once("XML/Parser.php"); 00013 00014 if(!defined("ATTRIBUTES")) { 00015 define("ATTRIBUTES", "__ATTRIBUTES__"); 00016 } 00017 00026 class BasicXmlParser extends XML_Parser { 00027 /*{{{ Members */ 00028 00029 protected $openTags; 00030 protected $docroot; 00031 00032 /*}}}*/ 00033 /*{{{ Constructor*/ 00034 00039 public function BasicXmlParser() { 00040 parent::XML_Parser(); 00041 } 00042 00043 /*}}}*/ 00044 /*{{{ public function handleAttrTag() */ 00045 00054 public function handleAttrTag($name, $attrs) { 00055 $tag = array(); 00056 foreach($attrs as $attr_name => $value) { 00057 $tag[$attr_name] = $value; 00058 } 00059 return $tag; 00060 } 00061 00062 /*}}}*/ 00063 /*{{{ public function startHandler() */ 00064 00073 function startHandler($xp, $name, $attributes) { 00074 $this->openTags[] = $name; 00075 } 00076 00077 /*}}}*/ 00078 /*{{{ public function endHandler()*/ 00079 00087 public function endHandler($xp, $name) { 00088 // Handle error tags 00089 $lastTag = $this->getLastOpenTag($name); 00090 switch($name) { 00091 case "MESSAGE": 00092 if($lastTag == "ERROR") { 00093 $this->docroot["ERROR"]["MESSAGE"] = $this->getCData(); 00094 } 00095 break; 00096 } 00097 // Empty CData 00098 $this->lastCData = ""; 00099 00100 // Close tag 00101 if($this->openTags[count($this->openTags)-1] == $name) { 00102 array_pop($this->openTags); 00103 } 00104 } 00105 00106 /*}}}*/ 00107 /*{{{ public function cdataHandler() */ 00108 00116 public function cdataHandler($xp, $cdata) { 00117 $this->lastCData .= $cdata; 00118 } 00119 00120 /*}}}*/ 00121 /*{{{ public function getCData() */ 00122 00129 public function getCData() { 00130 return $this->lastCData; 00131 } 00132 00133 /*}}}*/ 00134 /*{{{ public function getLastOpenTag() */ 00135 00143 public function getLastOpenTag($tag) { 00144 $lastTagIndex = count($this->openTags)-1; 00145 if($this->openTags[$lastTagIndex] == $tag) { 00146 if($lastTagIndex > 0) { 00147 return $this->openTags[$lastTagIndex-1]; 00148 } 00149 } 00150 return false; 00151 } 00152 00153 /*}}}*/ 00154 /*{{{ public function getDocumentArray() */ 00155 00164 public function getDocumentArray() { 00165 return $this->docroot; 00166 } 00167 00168 /*}}}*/ 00169 } 00170 00171 ?>