[Phpsysinfo-subversion] SF.net SVN: phpsysinfo:[251] includes/xml/class.SimpleXMLExtended.inc.php
Brought to you by:
namiltd
From: <big...@us...> - 2009-06-17 07:37:29
|
Revision: 251 http://phpsysinfo.svn.sourceforge.net/phpsysinfo/?rev=251&view=rev Author: bigmichi1 Date: 2009-06-17 07:37:15 +0000 (Wed, 17 Jun 2009) Log Message: ----------- SimpleXMLExtended - automatic utf8 conversation - cdata child - combine xml Added Paths: ----------- includes/xml/class.SimpleXMLExtended.inc.php Added: includes/xml/class.SimpleXMLExtended.inc.php =================================================================== --- includes/xml/class.SimpleXMLExtended.inc.php (rev 0) +++ includes/xml/class.SimpleXMLExtended.inc.php 2009-06-17 07:37:15 UTC (rev 251) @@ -0,0 +1,151 @@ +<?php +/** + * modified XML Element + * + * PHP version 5 + * + * @category PHP + * @package PSI_XML + * @author Michael Cramer <Big...@us...> + * @copyright 2009 phpSysInfo + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @version SVN: $Id$ + * @link http://phpsysinfo.sourceforge.net + */ + /** + * class extends the SimpleXML element for including some special functions, like encoding stuff and cdata support + * + * @category PHP + * @package PSI_XML + * @author Michael Cramer <Big...@us...> + * @copyright 2009 phpSysInfo + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @version Release: 3.0 + * @link http://phpsysinfo.sourceforge.net + */ +class SimpleXMLExtended +{ + /** + * store the encoding that is used for conversation to utf8 + * + * @var String base encoding + */ + private $_encoding = null; + + /** + * SimpleXMLElement to which every call is delegated + * + * @var SimpleXMLElement delegated SimpleXMLElement + */ + private $_SimpleXmlElement = null; + + /** + * create a new extended SimpleXMLElement and set encoding if specified + * + * @param SimpleXMLElement $xml base xml element + * @param String $encoding base encoding that should be used for conversation to utf8 + * + * @return void + */ + public function __construct($xml, $encoding = null) + { + if ($encoding != null) { + $this->_encoding = $encoding; + } + $this->_SimpleXmlElement = $xml; + } + + /** + * insert a child element with or without a value, also doing conversation of name and if value is set to utf8 + * + * @param String $name name of the child element + * @param String $value a value that should be insert to the child + * + * @return SimpleXMLExtended extended child SimpleXMLElement + */ + public function addChild($name, $value = null) + { + $nameUtf8 = $this->_toUTF8($name); + if ($value == null) { + return new SimpleXMLExtended($this->_SimpleXmlElement->addChild($nameUtf8), $this->_encoding); + } else { + $valueUtf8 = htmlspecialchars($this->_toUTF8($value)); + return new SimpleXMLExtended($this->_SimpleXmlElement->addChild($nameUtf8, $valueUtf8), $this->_encoding); + } + } + + /** + * insert a child with cdata section + * + * @param String $name name of the child element + * @param String $cdata data for CDATA section + * + * @return SimpleXMLExtended extended child SimpleXMLElement + */ + public function addCData($name, $cdata) + { + $nameUtf8 = $this->_toUTF8($name); + $node = $this->_SimpleXmlElement->addChild($nameUtf8); + $domnode = dom_import_simplexml($node); + $no = $domnode->ownerDocument; + $domnode->appendChild($no->createCDATASection($cdata)); + return new SimpleXMLExtended($node, $this->_encoding); + } + + /** + * add a attribute to a child and convert name and value to utf8 + * + * @param String $name name of the attribute + * @param String $value value of the attribute + * + * @return Void + */ + public function addAttribute($name, $value) + { + $nameUtf8 = $this->_toUTF8($name); + $valueUtf8 = htmlspecialchars($this->_toUTF8($value)); + $this->_SimpleXmlElement->addAttribute($nameUtf8, $valueUtf8); + } + + /** + * append a xml-tree to another xml-tree + * + * @param SimpleXMLElement $new_child child that should be appended + * + * @return Void + */ + public function combinexml(SimpleXMLElement $new_child) + { + $node1 = dom_import_simplexml($this->_SimpleXmlElement); + $dom_sxe = dom_import_simplexml($new_child); + $node2 = $node1->ownerDocument->importNode($dom_sxe, true); + $node1->appendChild($node2); + } + + /** + * convert a string into an UTF-8 string + * + * @param String $str string to convert + * + * @return String UTF-8 string + */ + private function _toUTF8($str) + { + if ($this->_encoding != null) { + return mb_convert_encoding(trim($str), 'UTF-8', $this->_encoding); + } else { + return mb_convert_encoding(trim($str), 'UTF-8'); + } + } + + /** + * Returns the SimpleXmlElement + * + * @return SimpleXmlElement entire xml as SimpleXmlElement + */ + public function getSimpleXmlElement() + { + return $this->_SimpleXmlElement; + } +} +?> Property changes on: includes/xml/class.SimpleXMLExtended.inc.php ___________________________________________________________________ Added: svn:mime-type + text/x-php Added: svn:keywords + Id Added: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |