[ Index ] |
|
Code source de PRADO 3.0.6 |
1 <?php 2 /** 3 * TXmlElement, TXmlDocument, TXmlElementList class file 4 * 5 * @author Qiang Xue <qiang.xue@gmail.com> 6 * @link http://www.pradosoft.com/ 7 * @copyright Copyright © 2005 PradoSoft 8 * @license http://www.pradosoft.com/license/ 9 * @version $Id: TXmlDocument.php 1397 2006-09-07 07:55:53Z wei $ 10 * @package System.Xml 11 */ 12 13 /** 14 * TXmlElement class. 15 * 16 * TXmlElement represents an XML element node. 17 * You can obtain its tagname, attributes, text between the openning and closing 18 * tags via the TagName, Attributes, and Value properties, respectively. 19 * You can also retrieve its parent and child elements by Parent and Elements 20 * properties, respectively. 21 * 22 * TBD: xpath 23 * 24 * @author Qiang Xue <qiang.xue@gmail.com> 25 * @version $Id: TXmlDocument.php 1397 2006-09-07 07:55:53Z wei $ 26 * @package System.Xml 27 * @since 3.0 28 */ 29 class TXmlElement extends TComponent 30 { 31 /** 32 * @var TXmlElement parent of this element 33 */ 34 private $_parent=null; 35 /** 36 * @var string tagname of this element 37 */ 38 private $_tagName='unknown'; 39 /** 40 * @var string text enclosed between openning and closing tags of this element 41 */ 42 private $_value=''; 43 /** 44 * @var TXmlElementList list of child elements of this element 45 */ 46 private $_elements=null; 47 /** 48 * @var TMap attributes of this element 49 */ 50 private $_attributes=null; 51 52 /** 53 * Constructor. 54 * @param string tagname for this element 55 */ 56 public function __construct($tagName) 57 { 58 $this->setTagName($tagName); 59 } 60 61 /** 62 * @return TXmlElement parent element of this element 63 */ 64 public function getParent() 65 { 66 return $this->_parent; 67 } 68 69 /** 70 * @param TXmlElement parent element of this element 71 */ 72 public function setParent($parent) 73 { 74 $this->_parent=$parent; 75 } 76 77 /** 78 * @return string tagname of this element 79 */ 80 public function getTagName() 81 { 82 return $this->_tagName; 83 } 84 85 /** 86 * @param string tagname of this element 87 */ 88 public function setTagName($tagName) 89 { 90 $this->_tagName=$tagName; 91 } 92 93 /** 94 * @return string text enclosed between opening and closing tag of this element 95 */ 96 public function getValue() 97 { 98 return $this->_value; 99 } 100 101 /** 102 * @param string text enclosed between opening and closing tag of this element 103 */ 104 public function setValue($value) 105 { 106 $this->_value=$value; 107 } 108 109 /** 110 * @return boolean true if this element has child elements 111 */ 112 public function getHasElement() 113 { 114 return $this->_elements!==null && $this->_elements->getCount()>0; 115 } 116 117 /** 118 * @return boolean true if this element has attributes 119 */ 120 public function getHasAttribute() 121 { 122 return $this->_attributes!==null && $this->_attributes->getCount()>0; 123 } 124 125 /** 126 * @return string the attribute specified by the name, null if no such attribute 127 */ 128 public function getAttribute($name) 129 { 130 if($this->_attributes!==null) 131 return $this->_attributes->itemAt($name); 132 else 133 return null; 134 } 135 136 /** 137 * @param string attribute name 138 * @param string attribute value 139 */ 140 public function setAttribute($name,$value) 141 { 142 $this->getAttributes()->add($name,$value); 143 } 144 145 /** 146 * @return TXmlElementList list of child elements 147 */ 148 public function getElements() 149 { 150 if(!$this->_elements) 151 $this->_elements=new TXmlElementList($this); 152 return $this->_elements; 153 } 154 155 /** 156 * @return TMap list of attributes 157 */ 158 public function getAttributes() 159 { 160 if(!$this->_attributes) 161 $this->_attributes=new TMap; 162 return $this->_attributes; 163 } 164 165 /** 166 * @return TXmlElement the first child element that has the specified tagname, null if not found 167 */ 168 public function getElementByTagName($tagName) 169 { 170 if($this->_elements) 171 { 172 foreach($this->_elements as $element) 173 if($element->_tagName===$tagName) 174 return $element; 175 } 176 return null; 177 } 178 179 /** 180 * @return TList list of all child elements that have the specified tagname 181 */ 182 public function getElementsByTagName($tagName) 183 { 184 $list=new TList; 185 if($this->_elements) 186 { 187 foreach($this->_elements as $element) 188 if($element->_tagName===$tagName) 189 $list->add($element); 190 } 191 return $list; 192 } 193 194 /** 195 * @return string string representation of this element 196 */ 197 public function toString($indent=0) 198 { 199 $attr=''; 200 if($this->_attributes!==null) 201 { 202 foreach($this->_attributes as $name=>$value) 203 $attr.=" $name=\"$value\""; 204 } 205 $prefix=str_repeat(' ',$indent*4); 206 if($this->getHasElement()) 207 { 208 $str=$prefix."<{$this->_tagName}$attr>\n"; 209 foreach($this->getElements() as $element) 210 $str.=$element->toString($indent+1)."\n"; 211 $str.=$prefix."</{$this->_tagName}>"; 212 return $str; 213 } 214 else if($this->getValue()!=='') 215 { 216 return $prefix."<{$this->_tagName}$attr>{$this->_value}</{$this->_tagName}>"; 217 } 218 else 219 return $prefix."<{$this->_tagName}$attr />"; 220 } 221 } 222 223 /** 224 * TXmlDocument class. 225 * 226 * TXmlDocument represents a DOM representation of an XML file. 227 * Besides all properties and methods inherited from {@link TXmlElement}, 228 * you can load an XML file or string by {@link loadFromFile} or {@link loadFromString}. 229 * You can also get the version and encoding of the XML document by 230 * the Version and Encoding properties. 231 * 232 * To construct an XML string, you may do the following: 233 * <code> 234 * $doc=new TXmlDocument('1.0','utf-8'); 235 * $doc->TagName='Root'; 236 * 237 * $proc=new TXmlElement('Proc'); 238 * $proc->setAttribute('Name','xxxx'); 239 * $doc->Elements[]=$proc; 240 * 241 * $query=new TXmlElement('Query'); 242 * $query->setAttribute('ID','xxxx'); 243 * $proc->Elements[]=$query; 244 * 245 * $attr=new TXmlElement('Attr'); 246 * $attr->setAttribute('Name','aaa'); 247 * $attr->Value='1'; 248 * $query->Elements[]=$attr; 249 * 250 * $attr=new TXmlElement('Attr'); 251 * $attr->setAttribute('Name','bbb'); 252 * $attr->Value='1'; 253 * $query->Elements[]=$attr; 254 * </code> 255 * The above code represents the following XML string: 256 * <code> 257 * <?xml version="1.0" encoding="utf-8"?> 258 * <Root> 259 * <Proc Name="xxxx"> 260 * <Query ID="xxxx"> 261 * <Attr Name="aaa">1</Attr> 262 * <Attr Name="bbb">1</Attr> 263 * </Query> 264 * </Proc> 265 * </Root> 266 * </code> 267 * 268 * @author Qiang Xue <qiang.xue@gmail.com> 269 * @version $Id: TXmlDocument.php 1397 2006-09-07 07:55:53Z wei $ 270 * @package System.Xml 271 * @since 3.0 272 */ 273 class TXmlDocument extends TXmlElement 274 { 275 /** 276 * @var string version of this XML document 277 */ 278 private $_version; 279 /** 280 * @var string encoding of this XML document 281 */ 282 private $_encoding; 283 284 /** 285 * Constructor. 286 * @param string version of this XML document 287 * @param string encoding of this XML document 288 */ 289 public function __construct($version='1.0',$encoding='') 290 { 291 parent::__construct(''); 292 $this->setversion($version); 293 $this->setEncoding($encoding); 294 } 295 296 /** 297 * @return string version of this XML document 298 */ 299 public function getVersion() 300 { 301 return $this->_version; 302 } 303 304 /** 305 * @param string version of this XML document 306 */ 307 public function setVersion($version) 308 { 309 $this->_version=$version; 310 } 311 312 /** 313 * @return string encoding of this XML document 314 */ 315 public function getEncoding() 316 { 317 return $this->_encoding; 318 } 319 320 /** 321 * @param string encoding of this XML document 322 */ 323 public function setEncoding($encoding) 324 { 325 $this->_encoding=$encoding; 326 } 327 328 /** 329 * Loads and parses an XML document. 330 * @param string the XML file path 331 * @return boolean whether the XML file is parsed successfully 332 * @throws TIOException if the file fails to be opened. 333 */ 334 public function loadFromFile($file) 335 { 336 if(($str=@file_get_contents($file))!==false) 337 return $this->loadFromString($str); 338 else 339 throw new TIOException('xmldocument_file_read_failed',$file); 340 } 341 342 /** 343 * Loads and parses an XML string. 344 * The version and encoding will be determined based on the parsing result. 345 * @param string the XML string 346 * @return boolean whether the XML string is parsed successfully 347 */ 348 public function loadFromString($string) 349 { 350 // TODO: since PHP 5.1, we can get parsing errors and throw them as exception 351 $doc=new DOMDocument(); 352 if($doc->loadXML($string)===false) 353 return false; 354 355 $this->setEncoding($doc->encoding); 356 $this->setVersion($doc->version); 357 358 $element=$doc->documentElement; 359 $this->setTagName($element->tagName); 360 $this->setValue($element->nodeValue); 361 $elements=$this->getElements(); 362 $attributes=$this->getAttributes(); 363 $elements->clear(); 364 $attributes->clear(); 365 foreach($element->attributes as $name=>$attr) 366 $attributes->add($name,$attr->value); 367 foreach($element->childNodes as $child) 368 { 369 if($child instanceof DOMElement) 370 $elements->add($this->buildElement($child)); 371 } 372 373 return true; 374 } 375 376 /** 377 * Saves this XML document as an XML file. 378 * @param string the name of the file to be stored with XML output 379 * @throws TIOException if the file cannot be written 380 */ 381 public function saveToFile($file) 382 { 383 if(($fw=fopen($file,'w'))!==false) 384 { 385 fwrite($fw,$this->saveToString()); 386 fclose($fw); 387 } 388 else 389 throw new TIOException('xmldocument_file_write_failed',$file); 390 } 391 392 /** 393 * Saves this XML document as an XML string 394 * @return string the XML string of this XML document 395 */ 396 public function saveToString() 397 { 398 $version=empty($this->_version)?' version="1.0"':' version="'.$this->_version.'"'; 399 $encoding=empty($this->_encoding)?'':' encoding="'.$this->_encoding.'"'; 400 return "<?xml{$version}{$encoding}?>\n".$this->toString(0); 401 } 402 403 /** 404 * Recursively converts DOM XML nodes into TXmlElement 405 * @param DOMXmlNode the node to be converted 406 * @return TXmlElement the converted TXmlElement 407 */ 408 private function buildElement($node) 409 { 410 $element=new TXmlElement($node->tagName); 411 $element->setValue($node->nodeValue); 412 foreach($node->attributes as $name=>$attr) 413 $element->getAttributes()->add($name,$attr->value); 414 foreach($node->childNodes as $child) 415 { 416 if($child instanceof DOMElement) 417 $element->getElements()->add($this->buildElement($child)); 418 } 419 return $element; 420 } 421 } 422 423 424 /** 425 * TXmlElementList class. 426 * 427 * TXmlElementList represents a collection of {@link TXmlElement}. 428 * You may manipulate the collection with the operations defined in {@link TList}. 429 * 430 * @author Qiang Xue <qiang.xue@gmail.com> 431 * @version $Id: TXmlDocument.php 1397 2006-09-07 07:55:53Z wei $ 432 * @package System.Xml 433 * @since 3.0 434 */ 435 class TXmlElementList extends TList 436 { 437 /** 438 * @var TXmlElement owner of this list 439 */ 440 private $_o; 441 442 /** 443 * Constructor. 444 * @param TXmlElement owner of this list 445 */ 446 public function __construct(TXmlElement $owner) 447 { 448 $this->_o=$owner; 449 } 450 451 /** 452 * @return TXmlElement owner of this list 453 */ 454 protected function getOwner() 455 { 456 return $this->_o; 457 } 458 459 /** 460 * Inserts an item at the specified position. 461 * This overrides the parent implementation by performing additional 462 * operations for each newly added TXmlElement object. 463 * @param integer the speicified position. 464 * @param mixed new item 465 * @throws TInvalidDataTypeException if the item to be inserted is not a TXmlElement object. 466 */ 467 public function insertAt($index,$item) 468 { 469 if($item instanceof TXmlElement) 470 { 471 parent::insertAt($index,$item); 472 if($item->getParent()!==null) 473 $item->getParent()->getElements()->remove($item); 474 $item->setParent($this->_o); 475 } 476 else 477 throw new TInvalidDataTypeException('xmlelementlist_xmlelement_required'); 478 } 479 480 /** 481 * Removes an item at the specified position. 482 * This overrides the parent implementation by performing additional 483 * cleanup work when removing a TXmlElement object. 484 * @param integer the index of the item to be removed. 485 * @return mixed the removed item. 486 */ 487 public function removeAt($index) 488 { 489 $item=parent::removeAt($index); 490 if($item instanceof TXmlElement) 491 $item->setParent(null); 492 return $item; 493 } 494 } 495 496 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |