/** * get files to be uploaded * @param object \SimpleXMLIterator $data data * @return array */ public function getFormUploadFields($data) { $data = new \SimpleXMLIterator($data->asXML()); if (isset($data->values->values->formId)) { $object = new \stdClass(); $object->id = $data->values->values->formId; $object->fields = array('xmlDefinition'); $res = $this->getOne($object); $formXml = file_get_contents('/var/www/htdocs/uploads/' . $res['fields']['xmlDefinition']); $formXml = new \SimpleXMLIterator($formXml); $uploadsFields = $formXml->xpath('//input[@type="file"]'); foreach ($uploadsFields as $field) { $controlName = (string) $field['name']; $control = $data->xpath('//' . $controlName); $fieldAnswers[] = array('fieldName' => $controlName, 'value' => (string) $control[0]); } return array('fields' => $fieldAnswers); } else { return false; } }
/** * @param \SimpleXMLIterator $sxi * * @param string $tableName * @param string $xpath */ protected function processValueOnly(\SimpleXMLIterator $sxi, string $tableName, string $xpath = '//result/child::*[not(*|@*|self::dataTime)]') { $items = $sxi->xpath($xpath); if (false === $items || 0 === count($items)) { return; } /** * @var \SimpleXMLElement[] $items */ $columns = []; foreach ($items as $ele) { $name = (string) $ele->getName(); $columns[$name] = $this->inferTypeFromName($name, true); } uksort($columns, function ($alpha, $beta) { return strtolower($alpha) <=> strtolower($beta); }); $this->tables[$tableName] = ['values' => $columns]; }
</animal> </document> XML; try { // load the XML Iterator and iterate over it $sxi = new SimpleXMLIterator($xmlstring); // iterate over animals foreach ($sxi as $animal) { // iterate over category nodes foreach ($animal as $key => $category) { echo $category->species . PHP_EOL; } } } catch (Exception $e) { die($e->getMessage()); } echo '===================================' . PHP_EOL; echo 'Finding all species with xpath' . PHP_EOL; echo '===================================' . PHP_EOL; // which can also be re-written for optimization try { // load the XML Iterator and iterate over it $sxi = new SimpleXMLIterator($xmlstring); // use xpath $foo = $sxi->xpath('animal/category/species'); foreach ($foo as $k => $v) { echo $v . PHP_EOL; } } catch (Exception $e) { die($e->getMessage()); }
/** * @param \SimpleXMLIterator $sxi * @param string $tableName * @param string $xpath */ protected function processValueOnly(\SimpleXMLIterator $sxi, string $tableName, string $xpath = '//result/child::*[not(*|@*|self::dataTime)]') { $items = $sxi->xpath($xpath); if (false === $items || 0 === count($items)) { return; } /** * @var \SimpleXMLElement[] $items */ $columns = []; foreach ($items as $ele) { $name = (string) $ele->getName(); $columns[$name] = $this->inferTypeFromName($name, true); } if ($this->hasOwner()) { $columns['ownerID'] = 'BIGINT(20) UNSIGNED NOT NULL'; } uksort($columns, function ($alpha, $beta) { return strtolower($alpha) <=> strtolower($beta); }); $keys = $this->getSqlKeys(); if (0 !== count($keys)) { $this->tables[$tableName] = ['columns' => $columns, 'keys' => $keys]; } else { $this->tables[$tableName] = ['columns' => $columns]; } }
//echo $name, '=>', $data, "<br />"; } // while 循环 $simpleIt->rewind(); while ($simpleIt->valid()) { /*var_dump($simpleIt->key()); echo '=>'; var_dump($simpleIt->current());*/ // getChildren() 获得当前节点的子节点 if ($simpleIt->hasChildren()) { //var_dump($simpleIt->getChildren()); } $simpleIt->next(); } // xpath 可以通过path直接获得指定节点的值 var_dump($simpleIt->xpath('animal/category/species')); } catch (Exception $e) { echo $e->getMessage(); } echo '--------------------------------- SimpleXMLIterator END-----------------------------------', '<br />'; echo '--------------------------------- CachingIterator START-----------------------------------', '<br />'; /** * CachingIterator 提前读取一个元素 * 可以用于确定当前元素是否为最后一个元素 */ $array = array('koala', 'kangaroo', 'wombat', 'wallaby', 'emu', 'kiwi', 'kookaburra', 'platypus'); try { $cachingIt = new CachingIterator(new ArrayIterator($array)); foreach ($cachingIt as $item) { echo $item; if ($cachingIt->hasNext()) {
/** * Extending the save action for custom attributes * check the orders, move order to higher rank and swap * @return void * @author Mudassar Mumtaz */ public function saveAction() { $data = array(); $result = new \SimpleXMLIterator($_POST['data']); $fieldset = $result->xpath("//*[contains(local-name(),'fieldset')]")[0]; $data['order'] = (int) $fieldset->label_order; $data['section'] = (string) $fieldset->label_section; $phpMethod = 'updateSectionOrder'; if ((string) $fieldset->label_customattribute_id != '') { $data['id'] = (string) $fieldset->label_customattribute_id; $phpMethod = 'swapSectionOrder'; } $this->_queueMessage(['phpMethod' => $phpMethod, 'phpClass' => 'MoveIn4CustomAttribute\\Model\\CustomAttribute', 'phpData' => $data]); $this->saveHelper($this->_formXml, $this->_handler, $this->_formCollection, true); return $this->setresponse(); }
\t\t\t<my:title>Крутая Сандра</my:title> \t\t\t<title>XML. Справочник</title> \t\t\t<price>100</price> \t\t</category> \t</book> </catalog> XML; ?> <?php /* $sxi =new SimpleXMLIterator($xmlstring); $foo = $sxi->xpath('/catalog/book/category/title'); foreach ($foo as $k=>$v){ echo $v.'<br />'; } */ ?> <?php $sxi = new SimpleXMLIterator($xmlstring); //$sxi-> registerXPathNamespace('my', 'http://mysuperpupermegasite.ru/catalog'); $result = $sxi->xpath('//my:title'); foreach ($sxi->getDocNamespaces('book') as $ns) { echo $ns . '<br>'; } foreach ($result as $k => $v) { echo $v . '<br>'; }