/** * * @return Zend_Tool_Project_Structure_Graph */ public function unserialize($data) { if ($data == null) { throw new Exception('contents not available to unserialize.'); } $xmlDataIterator = new SimpleXMLIterator($data); if ($xmlDataIterator->getName() != 'projectProfile') { throw new Exception('Profiles must start with a projectProfile node'); } $this->_graph = new Zend_Tool_Project_Structure_Graph(); $this->_unserializeRecurser($xmlDataIterator); return $this->_graph; }
/** * unserialize() * * Create a structure in the object $profile from the structure specficied * in the xml string provided * * @param string xml data * @param Zend_Tool_Project_Profile The profile to use as the top node * @return Zend_Tool_Project_Profile */ public function unserialize($data, Zend_Tool_Project_Profile $profile) { if ($data == null) { throw new Exception('contents not available to unserialize.'); } $this->_profile = $profile; $xmlDataIterator = new SimpleXMLIterator($data); if ($xmlDataIterator->getName() != 'extensionProfile') { throw new Exception('Extension profile must start with a extensionProfile node'); } if (isset($xmlDataIterator['type'])) { $this->_profile->setAttribute('type', (string) $xmlDataIterator['type']); } if (isset($xmlDataIterator['version'])) { $this->_profile->setAttribute('version', (string) $xmlDataIterator['version']); } // start un-serialization of the xml doc $this->_unserializeRecurser($xmlDataIterator); // contexts should be initialized after the unwinding of the profile structure $this->_lazyLoadContexts(); return $this->_profile; }
/** * unserialize() * * Create a structure in the object $profile from the structure specficied * in the xml string provided * * @param string xml data * @param Zend_Tool_Project_Profile The profile to use as the top node * @return Zend_Tool_Project_Profile */ public function unserialize($data, Zend_Tool_Project_Profile $profile) { if ($data == null) { throw new Exception('contents not available to unserialize.'); } $this->_profile = $profile; $xmlDataIterator = new SimpleXMLIterator($data); if ($xmlDataIterator->getName() != 'projectProfile') { throw new Exception('Profiles must start with a projectProfile node'); } $this->_unserializeRecurser($xmlDataIterator); $this->_lazyLoadContexts(); return $this->_profile; }
protected function _unserializeXml($xmlProjectProfile) { if ($xmlProjectProfile == null) { throw new Exception('contents not available to unserialize.'); } $projectProfileIterator = new SimpleXMLIterator($xmlProjectProfile); if ($projectProfileIterator->getName() != 'projectProfile') { throw new Exception('Profiles must start with a projectProfile node'); } $this->_unserializeRecurser($projectProfileIterator); }
<pre> <?php $xml = <<<XML <books> <book> <title>PHP Basics</title> <author>Jim Smith</author> </book> <book>XML basics</book> </books> XML; $xmlIterator = new SimpleXMLIterator($xml); //books for ($xmlIterator->rewind(); $xmlIterator->valid(); $xmlIterator->next()) { foreach ($xmlIterator->getChildren() as $name => $data) { echo "The {$name} is '{$data}' from the class " . get_class($data) . "\n"; } echo $xmlIterator->getName() . "\n"; //2 times of books will be print out } ?> </pre>
protected function updateAPP() { if (is_file($this->applist) && is_writable($this->applist)) { try { $doc = new SimpleXMLIterator($this->applist, null, true); } catch (Exception $e) { $this->error[] = "发生异常:" . $e->getMessage() . " 文件:" . $e->getFile() . " 行号:" . $e->getLine(); return false; } if ($doc->getName() != "apps") { $this->error[] = "applist.xml的根元素必须是apps"; return false; } $i = $doc->count(); if ($i !== 0) { $apps = $doc->app; foreach ($apps as $app) { if ($app['name'] == $this->app_name) { $this->error[] = "项目列表存在重名项目,请修改项目名称"; return false; } } } $doc->addChild("app"); $doc->app[$i]->addAttribute("name", $this->app_name); $doc->app[$i]->addAttribute("path", $this->app_path); $doc->app[$i]->addAttribute("index", $this->app_index); if ($doc->asXML($this->applist)) { return true; } else { $this->error[] = "TinkPHP助手/data/applist.xml 写入失败"; return false; } } else { $this->error[] = "TinkPHP助手/data/applist.xml不存在"; return false; } }
/** * Create an Array from XML * * This method sets up the SimpleXMLIterator and starts the parsing * of an xml body to iterate through it and transform it into * an array that can be used by the developers. * * @param string $xml * @return array An array mapped to the passed xml */ public static function arrayFromXml($xml) { // replace namespace defs $xml = str_replace('xmlns=', 'ns=', $xml); // catch libxml errors libxml_use_internal_errors(true); try { $iterator = new SimpleXMLIterator($xml); } catch(Exception $e) { $xmlErrors = libxml_get_errors(); return new Frapi_Exception( 'Xml Parsing Failed', 'INVALID_XML', 400, 'xml_parsing' ); libxml_clear_errors(); } $xmlRoot = $iterator->getName(); $type = $iterator->attributes()->type; // SimpleXML provides the root information on construct self::$_xmlRoot = $iterator->getName(); self::$_responseType = $type; // return the mapped array with the root element as the header return array($xmlRoot => self::_iteratorToArray($iterator)); }
/** * sets up the SimpleXMLIterator and starts the parsing * @access public * @param string $xml * @return array array mapped to the passed xml */ public static function arrayFromXml($xml) { // SimpleXML provides the root information on construct $iterator = new SimpleXMLIterator($xml); $xmlRoot = Braintree_Util::delimiterToCamelCase($iterator->getName()); $type = $iterator->attributes()->type; self::$_xmlRoot = $iterator->getName(); self::$_responseType = $type; // return the mapped array with the root element as the header return array($xmlRoot => self::_iteratorToArray($iterator)); }