simplexml


Iterating through and editing a SimpleXML object by reference in PHP


I am writing a function to decrypt the contents of a SimpleXML object in place.
function xmlWalkerDecrypt(&$xmlObj, $aesKey) {
if ($xmlObj->count()>0){
foreach($xmlObj as &$child){ //line 154
xmlWalkerDecrypt($child, $aesKey);
}
}
else{
$xmlObj = companyAES($xmlObj, $aesKey, 'decrypt');
}
}
This is throwing a the following error in WAMP:
Fatal error: An iterator cannot be used with foreach by reference in C:\wamp\www\_assets\walk.php on line 154
Is it possible to iterate through a SimpleXML object for the purpose of editing by reference, rather than outputting?
I've also tried:
function xmlWalkerDecrypt(&$xmlObj, $aesKey) {
if ($xmlObj->count()>0){
foreach($xmlObj->children() as &$child){ //line 154
xmlWalkerDecrypt($child, $aesKey);
}
}
else{
$xmlObj = companyAES($xmlObj, $aesKey, 'decrypt');
}
}
but that produces the following error:
Fatal error: Cannot create references to elements of a temporary array expression in C:\wamp\www_assets\myFile.php on line 154`
I am not sure what companyAES does to the variables passed to it but here are 2 points that might help:
Parameter 1 for xmlWalkerDecrypt does not need to be declared as a reference because all objects in PHP are passed by reference so there is no need to put that in your function declaration. You might think array() is an object but it isn't, but new ArrayObject() would create an object. See http://php.net/manual/en/class.arrayobject.php
You cannot modify a SimpleXMLElement, you can only read from or add attributes/children to it. If you need to modify the XML nodes once they are loaded/added you should use DOMDocument & it's related classes.
You most likely have the issue that you need to self-refence the element to change it:
$xmlObj[0] = companyAES($xmlObj, $aesKey, 'decrypt');
###
This is documented in an earlier question:
How can I set text value of SimpleXmlElement without using its parent?
Then additionally it's not necessary to pass those SimpleXMLElement objects by reference. So you can spare the aliasing within the foreach and the parameter definition :
function xmlWalkerDecrypt(SimpleXMLElement $xmlObj, $aesKey) {
...
foreach ($xmlObj->children() as $child) {
...
That should do it, this effectively takes care of the reported problem by the error message. Only existing variables can be aliased (&), not temporary array expressions.

Related Links

XML Node outside parent item
SimpleXMLElement cannot get the output
SimpleXML changing parsed decimals
can't navigate SimpleXMLElement data
PHP SimpleXML Load File with Null elements
Iterating through and editing a SimpleXML object by reference in PHP
Simplexml Insert into sub element
warning: simplexml_load_file(): I/O warning : failed to load external entity drupal [closed]
What's wrong with this code? SimpleXMLElement [closed]
Get children attributes using simplexml
Select attribute in simplexml
Save formatted xml
SimpleXML (Zend_Config_Xml actually) and foreach : which tag am I iterating?
simplexml_load_file() error happening in only localhost/xampp
SimpleXml tar.gz
simplexml_load_file error in PHP 5.3

Categories

HOME
asp.net-core
cplex
spagobi
callback
ebean
gnupg
metatrader4
kentor-authservices
jplayer
circular-dependency
circuit
foselasticabundle
shared-hosting
nas
conemu
arabic
captiveportal
su
chromebook
react-css-modules
virtualdub
uninstall
entitlements
linkerd
web-api-testing
social-media
maxmind
saas
libssl
tasklet
nouislider
twilio-api
.net-4.0
xquery-3.0
titanium-mobile
google-qpx-express-api
winrt-xaml-toolkit
repo
ghost4j
http-referer
mpmediaquery
hexo
host
restlet
disassembling
jquery-bootgrid
xcode-extension
lto
dynamics-crm-2013
unspecified
reportbuilder
webdriver-manager
unixodbc
gpx
websphere-mq-fte
smartcontracts
revapi
rails-routing
aurelia-validation
nstextview
brightcove
nativeapplication
forever
google-perftools
pillow
jspdf-autotable
spring-cache
hittest
minimization
mediaelement
objective-c-swift-bridge
static-ip-address
asp.net-4.5
qgraphicsview
pickadate
vhd
teamcity-8.0
python-3.2
simple-framework
ionic
qpainter
tt-news
javafx-webengine
internet-connection
rdl
document-classification
undefined-reference
viadeo
gnu-smalltalk
floating-point-precision
geos
sitemesh
gwt-rpc
eventual-consistency
android-screen-support
tinn-r
tridion-worldserver
objective-c-2.0
getmessage
parametric-equations
pendrive
pydot
locate
gdlib
mysql-error-1005
lpeg
html-input
coda-slider
javap
data-driven
mediarss
ugc
wsdl.exe

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App