/** * Create the ResultSet * * @param object $result * @return void */ public function __construct($result) { $xmlIterator = new \SimpleXMLIterator($result); for ($xmlIterator->rewind(); $xmlIterator->valid(); $xmlIterator->next()) { $temp = new \stdClass(); foreach ($xmlIterator->getChildren()->attributes() as $name => $value) { switch ($name) { case 'orderId': case 'brokerTicketId': case 'quantity': case 'purchaseOrderId': $temp->{$name} = (int) $value; break; case 'cost': $temp->{$name} = (double) $value; break; case 'electronicDelivery': $temp->{$name} = (bool) $value; break; default: $temp->{$name} = (string) $value; } } $this->_results[] = $temp; } unset($xmlIterator, $temp, $name, $value); }
function testChildCountDuringIteration() { $s = new SimpleXMLIterator(basicXML()); echo "Counts of first level nodes: "; foreach ($s as $i => $e) { echo "{$i}: {$s->getChildren()->count()}, "; } echo "\n"; }
function get_elements_from_supplied_file($file) { $found_elements = array(); if ($xml = file_get_contents($file)) { //print_r($xml); //SimpleXMLIterator just runs over the xml and returns the elements found in this file. I think this is just top level //which is what I want. //echo $xml; /* Some safety against XML Injection attack * see: http://phpsecurity.readthedocs.org/en/latest/Injection-Attacks.html * * Attempt a quickie detection of DOCTYPE - discard if it is present (cos it shouldn't be!) */ $collapsedXML = preg_replace("/[[:space:]]/", '', $xml); //echo $collapsedXML; if (preg_match("/<!DOCTYPE/i", $collapsedXML)) { //throw new InvalidArgumentException( // 'Invalid XML: Detected use of illegal DOCTYPE' // ); //echo "fail"; return FALSE; } $loadEntities = libxml_disable_entity_loader(true); $dom = new DOMDocument(); $dom->loadXML($xml); foreach ($dom->childNodes as $child) { if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) { throw new Exception\ValueException('Invalid XML: Detected use of illegal DOCTYPE'); libxml_disable_entity_loader($loadEntities); return FALSE; } } libxml_disable_entity_loader($loadEntities); //Iterate over elements now if (simplexml_import_dom($dom)) { $xmlIterator = new SimpleXMLIterator($xml); 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"; $found_elements[] = $name; } } } else { return FALSE; } } return $found_elements; }
/** * _unserializeRecurser() * * This method will be used to traverse the depths of the structure * as needed to *unserialize* the profile from an xmlIterator * * @param SimpleXMLIterator $xmlIterator * @param Zend_Tool_Project_Profile_Resource $resource */ protected function _unserializeRecurser(SimpleXMLIterator $xmlIterator, Zend_Tool_Project_Profile_Resource $resource = null) { foreach ($xmlIterator as $resourceName => $resourceData) { $contextName = $resourceName; $subResource = new Zend_Tool_Project_Profile_Resource($contextName); $subResource->setProfile($this->_profile); if ($resourceAttributes = $resourceData->attributes()) { $attributes = array(); foreach ($resourceAttributes as $attrName => $attrValue) { $attributes[$attrName] = (string) $attrValue; } $subResource->setAttributes($attributes); } if ($resource) { $resource->append($subResource, false); } else { $this->_profile->append($subResource); } if ($this->_contextRepository->isOverwritableContext($contextName) == false) { $subResource->initializeContext(); } if ($xmlIterator->hasChildren()) { self::_unserializeRecurser($xmlIterator->getChildren(), $subResource); } } }
protected function _unserializeRecurser(SimpleXMLIterator $projectProfileIterator, Zend_Tool_Provider_ZfProject_ProjectContext_ProjectContextAbstract $context = null) { foreach ($projectProfileIterator as $projectProfileNodeName => $projectProfileNode) { $subContextClass = self::getContextByName($projectProfileNodeName); $subContext = new $subContextClass(); if ($subContext->getContextName() === 'projectProfileFile') { $subContext->setProjectProfile($this); } if ($attributes = $projectProfileNode->attributes()) { foreach ($attributes as $attrName => $attrValue) { $clnAttrs[$attrName] = (string) $attrValue; } $subContext->setParameters($clnAttrs); } if ($context) { $context->append($subContext); } else { $this->appendProjectContext($subContext); } if ($projectProfileIterator->hasChildren()) { self::_unserializeRecurser($projectProfileIterator->getChildren(), $subContext); } } }
<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>
if (!$quiet) { printf("Done." . PHP_EOL . "Getting cash import file list ... "); } $files = get_files(); if (!$files) { unlink(COOKIE_FILE); die("Cannot get file list!" . PHP_EOL); } if (!$quiet) { printf("Done." . PHP_EOL); } $xml = new SimpleXMLIterator($files); $xml->rewind(); while ($xml->valid()) { if ($xml->key() == "rows") { $rows = $xml->getChildren(); $rows->rewind(); while ($rows->valid()) { if ($rows->key() == "row") { $fileid = $filename = NULL; $props = $rows->getChildren(); $props->rewind(); while ($props->valid()) { switch ($props->key()) { case "id": $fileid = strval($props->current()); break; case "file_name": $filename = strval($props->current()); } $props->next();
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/12/1 * Time: 11:29 */ $it = new SimpleXMLIterator(file_get_contents('test.xml')); foreach ($it as $key => $node) { echo $key . "<br/>"; if ($it->hasChildren()) { foreach ($it->getChildren() as $element => $value) { echo "\t" . $element . " : " . $value . "<br/>"; } } }
function getChildren() { echo __METHOD__ . "\n"; return parent::getChildren(); }
/** * * Realiza busca no xmlRoot pelas referências de arquivo * @return \stdClass */ public function find() { $get = self::$_config['attrs']->src->root; $xmlIterator = new SimpleXMLIterator(self::$_config['valueObject']->get()); $references = ""; for ($xmlIterator->rewind(); $xmlIterator->valid(); $xmlIterator->next()) { foreach ($xmlIterator->getChildren() as $name => $data) { $references .= '<reference>' . $data . '</reference>'; } } $result = new \stdClass(); $result->references = $references; return $result; }
$strLine = ''; logError(0, $strDescription, $strLine, 2); flushExit(); exit; } else { $arrXmlSection[$strSection] = $nXmlSection; $nXmlSection++; $nXmlSectionKey = 0; } if ($bLogSetupIni) { // Log XML-setup section $strDescription = '-- XML-setup section "' . $xmlSetupIterator->key() . '"' . PHP_EOL; $strLine = ''; logLog(0, $strDescription, $strLine, $nLogModeSetupXml); } foreach ($xmlSetupIterator->getChildren() as $strKey => $strValue) { // XML-setup element $strKey2 = '[' . $strKey . ']'; $strValue = trim($strValue); if (isset($arrXmlSectionKey[$strSection][$strKey])) { // Error: duplicate XML-setup [section][key] $strDescription = PHP_EOL . '## error: duplicate XML-setup [section][key]: "' . PHP_EOL; $strLine = $strSection2 . $strKey2 . ' ##' . PHP_EOL; logError(0, $strDescription, $strLine, 2); $strDescription = '> PHP EXIT <' . PHP_EOL; $strLine = ''; logError(0, $strDescription, $strLine, 2); flushExit(); exit; } else { $arrXmlSectionKey[$strSection][$strKey] = $nXmlSectionKey;
protected function _unserializeRecurser(SimpleXMLIterator $xmlIterator, Zend_Tool_Project_Structure_Node $node = null) { foreach ($xmlIterator as $nodeName => $nodeData) { $context = Zend_Tool_Project_Structure_Context_Registry::getInstance()->getContext($nodeName); $subNode = new Zend_Tool_Project_Structure_Node($context); if ($attributes = $nodeData->attributes()) { foreach ($attributes as $attrName => $attrValue) { $method = 'set' . $attrName; if (method_exists($subNode, $method)) { $subNode->{$method}((string) $attrValue); } elseif (method_exists($context, $method)) { $context->{$method}((string) $attrValue); } } } if (method_exists($context, 'setGraph')) { $context->setGraph($this->_graph); } if ($node) { $node->append($subNode); } else { $this->_graph->append($subNode); } if ($xmlIterator->hasChildren()) { self::_unserializeRecurser($xmlIterator->getChildren(), $subNode); } } }