Menu

[r21]: / trunk / src / parser / BasicXmlParser.php  Maximize  Restore  History

Download this file

172 lines (148 with data), 4.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/*
* $Id$
*
* Copyright(c) 2004-2006, SpikeSource Inc. All Rights Reserved.
* Licensed under the Open Software License version 2.1
* (See http://www.spikesource.com/license.html)
*/
?>
<?php
require_once("XML/Parser.php");
if(!defined("ATTRIBUTES")) {
define("ATTRIBUTES", "__ATTRIBUTES__");
}
/**
* An XML parser that extends the functionality of PEAR XML_Parser
* module.
*
* @author Nimish Pachapurkar <npac@spikesource.com>
* @version $Revision: $
* @package SpikePHPCoverage_Parser
*/
class BasicXmlParser extends XML_Parser {
/*{{{ Members */
protected $openTags;
protected $docroot;
/*}}}*/
/*{{{ Constructor*/
/**
* Constructor
* @access public
*/
public function BasicXmlParser() {
parent::XML_Parser();
}
/*}}}*/
/*{{{ public function handleAttrTag() */
/**
* Function that handles an element with attributes.
*
* @param $name Name of the element
* @param $attrs Attributes array (name, value pairs)
* @return Array An element
* @access public
*/
public function handleAttrTag($name, $attrs) {
$tag = array();
foreach($attrs as $attr_name => $value) {
$tag[$attr_name] = $value;
}
return $tag;
}
/*}}}*/
/*{{{ public function startHandler() */
/**
* Function to handle start of an element
*
* @param $xp XMLParser handle
* @param $name Element name
* @param $attributes Attribute array
* @access public
*/
function startHandler($xp, $name, $attributes) {
$this->openTags[] = $name;
}
/*}}}*/
/*{{{ public function endHandler()*/
/**
* Function to handle end of an element
*
* @param $xp XML_Parser handle
* @param $name Name of the element
* @access public
*/
public function endHandler($xp, $name) {
// Handle error tags
$lastTag = $this->getLastOpenTag($name);
switch($name) {
case "MESSAGE":
if($lastTag == "ERROR") {
$this->docroot["ERROR"]["MESSAGE"] = $this->getCData();
}
break;
}
// Empty CData
$this->lastCData = "";
// Close tag
if($this->openTags[count($this->openTags)-1] == $name) {
array_pop($this->openTags);
}
}
/*}}}*/
/*{{{ public function cdataHandler() */
/**
* Function to handle character data
*
* @param $xp XMLParser handle
* @param $cdata Character data
* @access public
*/
public function cdataHandler($xp, $cdata) {
$this->lastCData .= $cdata;
}
/*}}}*/
/*{{{ public function getCData() */
/**
* Returns the CData collected so far.
*
* @return String Character data collected.
* @access public
*/
public function getCData() {
return $this->lastCData;
}
/*}}}*/
/*{{{ public function getLastOpenTag() */
/**
* Returns the name of parent tag of give tag
*
* @param $tag Name of a child tag
* @return String Name of the parent tag of $tag
* @access public
*/
public function getLastOpenTag($tag) {
$lastTagIndex = count($this->openTags)-1;
if($this->openTags[$lastTagIndex] == $tag) {
if($lastTagIndex > 0) {
return $this->openTags[$lastTagIndex-1];
}
}
return false;
}
/*}}}*/
/*{{{ public function getDocumentArray() */
/**
* Return the document array gathered during parsing.
* Document array is a data structure that mimics the XML
* contents.
*
* @return Array Document array
* @access public
*/
public function getDocumentArray() {
return $this->docroot;
}
/*}}}*/
}
?>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.