/** * Lazy wrapper for the current element * * (PHP 5 >= 5.0.0)<br/> * Return the current element * @link http://php.net/manual/en/iterator.current.php * @return Aoe_AttributeConfigurator_Model_Config_Attribute */ public function current() { if ($this->_current) { return $this->_current; } $current = $this->_createModelFromNode($this->_xmlIterator->current()); $this->_current = $current; return $current; }
public function current() { $current = parent::current(); $namespaces = self::$NAMESPACES[spl_object_hash($this)]; $iso = self::_iso(); return new Qi_Xml($current, $namespaces, $iso); }
protected function execute(InputInterface $input, OutputInterface $output) { $fileInfo = new \SplFileInfo($this->getContainer()->getParameter('kernel.root_dir') . '/../web/sitemap.xml'); if ($fileInfo->isFile() && $fileInfo->isReadable()) { $output->write('Reading sitemap.xml...'); $file = $fileInfo->openFile(); $xml = ''; while (!$file->eof()) { $xml .= $file->fgets(); } $output->writeln(' done.'); $output->write('Updating sitemap.xml...'); $sitemap = new \SimpleXMLIterator($xml); $sitemap->rewind(); $lastmodDate = new \DateTime(); $lastmodDate->sub(new \DateInterval('P1D')); $lastmodDateFormatted = $lastmodDate->format('Y-m-d'); while ($sitemap->valid()) { $sitemap->current()->lastmod = $lastmodDateFormatted; $sitemap->next(); } $file = $file->openFile('w'); $file->fwrite($sitemap->asXML()); $output->writeln(' done.'); } else { $output->writeln('Error: Cannot open file web/sitemap.xml'); } }
private function xmlToArray(\SimpleXMLIterator $sxi) { $a = array(); for ($sxi->rewind(); $sxi->valid(); $sxi->next()) { $t = array(); $current = $sxi->current(); $attributes = $current->attributes(); $name = isset($attributes->_key) ? strval($attributes->_key) : $sxi->key(); // save attributes foreach ($attributes as $att_key => $att_value) { if ($att_key !== '_key') { $t[$att_key] = strval($att_value); } } // we parse nodes if ($sxi->hasChildren()) { // children $t = array_merge($t, $this->xmlToArray($current)); } else { // it's a leaf if (empty($t)) { $t = strval($current); // strval will call _toString() } else { $t['_value'] = strval($current); // strval will call _toString() } } $a[$name] = $t; } return $a; }
/** * Converts SimpleXMLIterator object into an array. * @param SimpleXMLIterator $xmlIterator * @return string[] */ public function simplexmlToArray($xmlIterator) { $xmlStringArray = array(); for ($xmlIterator->rewind(); $xmlIterator->valid(); $xmlIterator->next()) { if ($xmlIterator->hasChildren()) { $object = $xmlIterator->current(); $xmlStringArray[$object->getName()] = $this->simplexmlToArray($object); } else { $object = $xmlIterator->current(); $xmlStringArray[$object->getName()] = (string) $xmlIterator->current(); } } return $xmlStringArray; }
<pre> <?php $xmlIterator = new SimpleXMLIterator('<books><book>PHP basics</book><book>XML basics</book></books>'); var_dump($xmlIterator->current()); $xmlIterator->rewind(); // rewind to first element var_dump($xmlIterator->current()); /** * NULL object(SimpleXMLIterator)#2 (0) { } */ ?> </pre>
protected function parseDependencyType(\SimpleXMLIterator $iterator, DependencyCollection $dependencies, $required = true) { $iterator->rewind(); while ($iterator->valid()) { $elt = $iterator->current(); $name = $elt->getName(); $iterator->next(); if ($name === 'php') { $dependencies->import($this->parsePhpCondition($elt)); } elseif ($name === 'pearinstaller') { // Let's just ignore this for now! } else { // TODO do not ignore recommended, nodefault and uri, providesextension $identifier = 'pear2://' . (string) $elt->channel . '/' . (string) $elt->name; $dependencies[] = new Dependency($identifier, isset($elt->min) ? (string) $elt->min : null, isset($elt->max) ? (string) $elt->max : null, isset($elt->conflicts) ? Dependency::CONFLICT : ($required ? Dependency::REQUIRED : Dependency::OPTIONAL)); foreach ($elt->exclude as $exclude) { $dependencies[] = new Dependency($identifier, (string) $exclude, (string) $exclude, Dependency::CONFLICT); } } } }
// Log open XML-setup filename $strDescription = '- open XML-setup filename "' . $filenameXmlSetup . '"' . PHP_EOL; $strLine = ''; logLog(0, $strDescription, $strLine, 2); } // parse through XML-setup and store values in for ($xmlSetupIterator->rewind(); $xmlSetupIterator->valid(); $xmlSetupIterator->next()) { // $arrXmlSection[<[section]>] = <0..n> | Section order from top to bottom // $arrXmlSectionKey[<[section]>][<key>] = <0..n> | Key order from top to bottom // $arrXmlParameter[<key>] = <value> // $nXmlSection // $nXmlSectionKey switch ($xmlSetupIterator->key()) { case 'special_char': // Log special characters $strDescription = '-- special characters "' . $xmlSetupIterator->current() . '"' . PHP_EOL; $strLine = ''; logLog(0, $strDescription, $strLine, $nLogModeSetupXml); break; case 'parameter': // Log parameters // $strDescription = '-- parameter "' . $xmlSetupIterator->current() . '"' . PHP_EOL; // $strLine = ''; // logLog(0, $strDescription, $strLine, 2); break; default: if (!$xmlSetupIterator->hasChildren()) { // XML-setup error, no exit if (2 > $nbXmlSetupError) { $nbXmlSetupError = 1; }
function current() { echo __METHOD__ . "\n"; return parent::current(); }
<pre> <?php $xmlIterator = new SimpleXMLIterator('<books><book>PHP basics</book><book>XML basics</book></books>'); print_r($xmlIterator->current()); echo var_dump($xmlIterator->key()); //bool(false) $xmlIterator->rewind(); // rewind to the first element print_r($xmlIterator->current()); echo var_dump($xmlIterator->key()); //string(4) "book" ?> </pre>
/** * @param SimpleXMLIterator $sxi */ public function sxiToArray($sxi) { $a = array(); for ($sxi->rewind(); $sxi->valid(); $sxi->next()) { if (!array_key_exists($sxi->key(), $a)) { $a[$sxi->key()] = array(); } if ($sxi->hasChildren()) { $a[$sxi->key()]['children'] = $this->sxiToArray($sxi->current()); } else { $a[$sxi->key()][] = strval($sxi->current()); } } return $a; }
var_dump($xml->asXML()); /* string(181) "<?xml version="1.0"?> <application name="ls"> <param long="list">l</param> <param>a</param> <help> <cmd>h</cmd> </help> <new-node>1</new-node> </application> */ //---------- $iterator = new SimpleXMLIterator($string); foreach ($iterator as $key => $value) { echo "key: {$key}, value: {$value}\n"; if ($iterator->hasChildren()) { var_dump($iterator->current()); } } /* key: param, value: l key: param, value: a key: help, value: object(SimpleXMLIterator)#5 (1) { ["cmd"]=> string(1) "h" } */
/** * Converts a SimpleXMLIterator structure into an associative array. * * Used to parse an XML response from Mollom servers into a PHP array. For * example: * @code * $elements = new SimpleXmlIterator($response_body); * $parsed_response = $this->parseXML($elements); * @endcode * * @param SimpleXMLIterator $sxi * A SimpleXMLIterator structure of the server response body. * * @return array * An associative, possibly multidimensional array. */ public static function parseXML(SimpleXMLIterator $sxi) { $a = array(); $remove = array(); for ($sxi->rewind(); $sxi->valid(); $sxi->next()) { $key = $sxi->key(); // Recurse into non-scalar values. if ($sxi->hasChildren()) { $value = self::parseXML($sxi->current()); } else { $value = strval($sxi->current()); } if (!isset($a[$key])) { $a[$key] = $value; } else { // First time we reach here, convert the existing keyed item. Do not // remove $key, so we enter this path again. if (!isset($remove[$key])) { $a[] = $a[$key]; // Mark $key for removal. $remove[$key] = $key; } // Add the new item. $a[] = $value; } } // Lastly, remove named keys that have been converted to indexed keys. foreach ($remove as $key) { unset($a[$key]); } return $a; }
/** * Parses object. Turns raw XML(NPRML) into various object properties. */ function parse() { if (!empty($this->xml)) { $xml = $this->xml; } else { $this->notices[] = 'No XML to parse.'; return; } $object = simplexml_load_string($xml); $this->add_simplexml_attributes($object, $this); if (!empty($object->message)) { $this->message->id = $this->get_attribute($object->message, 'id'); $this->message->level = $this->get_attribute($object->message, 'level'); } if (!empty($object->list->story)) { foreach ($object->list->story as $story) { $parsed = new NPRMLEntity(); $this->add_simplexml_attributes($story, $parsed); //Iterate trough the XML document and list all the children $xml_iterator = new SimpleXMLIterator($story->asXML()); $key = NULL; $current = NULL; for ($xml_iterator->rewind(); $xml_iterator->valid(); $xml_iterator->next()) { $current = $xml_iterator->current(); $key = $xml_iterator->key(); if ($key == 'image' || $key == 'audio' || $key == 'link') { // images if ($key == 'image') { $parsed->{$key}[] = $this->parse_simplexml_element($current); } // audio if ($key == 'audio') { $parsed->{$key}[] = $this->parse_simplexml_element($current); } // links if ($key == 'link') { $type = $this->get_attribute($current, 'type'); $parsed->{$key}[$type] = $this->parse_simplexml_element($current); } } else { //if ->$key exist, see if it's an array. if it is, add the next child. if (!empty($parsed->{$key})) { //if it's not an array, make an array, add the existing element to it if (!is_array($parsed->{$key})) { $parsed->{$key} = array($parsed->{$key}); } // then add the new child. $parsed->{$key}[] = $this->parse_simplexml_element($current); } else { //The key wasn't parsed already, so just add the current element. $parsed->{$key} = $this->parse_simplexml_element($current); } } } $body = ''; if (!empty($parsed->textWithHtml->paragraphs)) { foreach ($parsed->textWithHtml->paragraphs as $paragraph) { $body = $body . $paragraph->value . "\n\n"; } } $parsed->body = $body; $this->stories[] = $parsed; } //if the query didn't have a sort parameter, reverse the order so that we end up with //stories in reverse-chron order. //there are no params and 'sort=' is not in the URL if (empty($this->request->params) && !stristr($this->request->request_url, 'sort=')) { $this->stories = array_reverse($this->stories); } //there are params, and sort is not one of them if (!empty($this->request->params) && !array_key_exists('sort', $this->request->params)) { $this->stories = array_reverse($this->stories); } } }
private function ParseXML($data_content) { $xmlIterator = new SimpleXMLIterator($data_content); $xmlIterator->rewind(); $xmlIterator->next(); while ($xmlIterator->current() != Null) { $current = $xmlIterator->current(); $Item = $this->ParsePerItem($current); $Parsed[$Item["sku"]] = $Item; $xmlIterator->next(); } return $Parsed; }
/** * @return toba_importador_plan_item */ function current() { $xml_item = $this->it_actual->current(); $item_plan = new toba_importador_plan_item($this->path_metadatos, $this->tipo_actual, (string) $xml_item['id'], (string) $xml_item['path']); return $item_plan; }
/** * Convert XML iterator to array * * @static * @param \SimpleXMLIterator $xml * @param null $ns * @return array */ public static function xmlToArray(\SimpleXMLIterator $xml, $ns = null) { $a = []; for ($xml->rewind(); $xml->valid(); $xml->next()) { $key = $xml->key(); if (!isset($a[$key])) { $a[$key] = []; } foreach ($xml->current()->attributes() as $k => $v) { $a[$key][self::INDEX_ATTRIBUTES][$k] = (string) $v; } if ($ns) { foreach ($ns as $name) { foreach ($xml->current()->attributes($name) as $k => $v) { $a[$key][self::INDEX_ATTRIBUTES][$k] = (string) $v; } } } if ($xml->hasChildren()) { $a[$key][self::INDEX_CONTENT] = self::xmlToArray($xml->current(), $ns); } else { $a[$key][self::INDEX_CONTENT] = strval($xml->current()); } } return $a; }