本文整理汇总了PHP中SimpleXMLIterator类的典型用法代码示例。如果您正苦于以下问题:PHP SimpleXMLIterator类的具体用法?PHP SimpleXMLIterator怎么用?PHP SimpleXMLIterator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SimpleXMLIterator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: parseFile
/**
* Parse csv file for getting its metadata
* @param \SimpleXMLIterator $data data
* @return multitype:Ambigous <>
*/
public function parseFile($data)
{
try {
$temp = (array) $data;
$fileName = $temp['fileName'];
$dataType = $temp['dataType'];
$duplicateHandle = $data['duplicateHandle'];
$fileStorageId = (string) $temp['fileStorageId'];
$fullPath = (string) $temp['fullPath'];
// count file
$fp = file($fullPath);
$count = count($fp) - 1;
// Get headers
$header = array();
if (($handle = fopen($fullPath, "r")) !== false) {
// no row length limit plus csv escape with '
while (($csvRow = fgetcsv($handle, 0, ",", "'")) !== false) {
for ($columnNo = 0, $columnCount = count($csvRow); $columnNo < $columnCount; $columnNo++) {
$header[] = $csvRow[$columnNo];
$data->headers->addChild($columnNo, (string) $csvRow[$columnNo]);
}
break;
}
fclose($handle);
}
$data->records = $count;
$file = $data->addChild('file');
$file->addChild('fileName', (string) $temp['fileName']);
$file->addChild('fileStorageId', $fileStorageId);
unset($data->fileStorageId);
unset($data->fileName);
unset($data->fullPath);
$response = $this->edit($data);
// Make response
$response['fileName'] = $fileName;
$response['headers'] = $header;
return $response;
} catch (\Exception $e) {
echo $e->getMessage();
}
}
示例2: edit
/**
* save and edit system template document
* @param unknown $data data
* @return Ambigous <multitype:, multitype:Ambigous <boolean, \Base\Model\Service\Ambigous, multitype:, string, unknown, object> NULL >
*/
public function edit($data)
{
if (isset($data->id) && !empty($data->id) && isset($data->generatedOn)) {
unset($data->recipient);
}
$result = parent::edit($data);
$response = new \SimpleXMLIterator('<?xml version="1.0" encoding="UTF-8"?><template/>');
$response->addChild('id', (string) $result['id']);
$response->addChild('fields', 'recipient');
$recipients = $this->getOne($response);
$result['recipients'] = $recipients['fields'];
return $result;
}
示例3: get_elements_from_supplied_file
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;
}
示例4: _XMLNodeToArray
/**
* Recursively convert \SimpleXMLIterator to array
*
* @param \SimpleXMLIterator $XMLNode
*
* @return array
*/
private static function _XMLNodeToArray($XMLNode)
{
$result = array();
$attributes = $XMLNode->attributes();
foreach ($attributes as $k => $v) {
$val = (string) $v;
if ($val == "True" || $val == "False") {
$val = (bool) $val;
}
$result[$k] = $val;
}
$children = $XMLNode->children();
foreach ($children as $chK => $chNode) {
$result['Items'][] = self::_XMLNodeToArray($chNode);
}
return $result;
}
示例5: xmlToArray
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;
}
示例6: getFormUploadFields
/**
* 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;
}
}
示例7: execute
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');
}
}
示例8: __construct
/**
* 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);
}
示例9: unserialize
/**
* 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;
}
示例10: SimpleXMLIterator
<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>
示例11: unlink
if (!$res) {
unlink(COOKIE_FILE);
die("Cannot log in to BGZ!" . PHP_EOL);
}
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;
示例12: parseDependencyType
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);
}
}
}
}
示例13: SimpleXMLIterator
<?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/>";
}
}
}
示例14: _unserializeRecurser
/**
* _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);
}
}
}
示例15: ParseXML
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;
}