simplexml


SimpleXML (Zend_Config_Xml actually) and foreach : which tag am I iterating?


I'm implementing a little event manager in order to use the Observer pattern. To subscribe my observers to my events, I'm using the following xml file :
<?xml version="1.0" encoding="UTF-8"?>
<configData>
<subscriptions>
<subscription>
<eventName>event_name</eventName>
<class>My_Observer_Class</class>
<function>myFunction</function>
</subscription>
<subscription>
<eventName>other_event_name</eventName>
<class>My_Observer_Otherclass</class>
<function>myOtherFunction</function>
</subscription>
</subscriptions>
</configData>
I'm using a foreach to loop on the subscriptions :
foreach($subscriptions->subscription as $subscription) {
/* using $subscription->eventName etc... */
}
And everything is ok, each $subscription item has it's eventName etc...
But here comes my problem :
<?xml version="1.0" encoding="UTF-8"?>
<configData>
<subscriptions>
<subscription>
<eventName>event_name</eventName>
<class>My_Observer_Class</class>
<function>myFunction</function>
</subscription>
</subscriptions>
</configData>
Here I have only one <subscription> node. And my foreach loops on the subscription children !
To solve this problem, I'd like to know how I can check if the xml file contains several <subscription> tags, or just one...
Any help will be appreciated :)
Edit : Is there a way to use xpath with my Zend_Config_Xml object ?
Just to clarify, this is an issue with Zend_Config_XML which is not present in PHP's native SimpleXML.
Given your second example as $xml, I can run the following and get the word 'subscription' as expected:
$configData = simplexml_load_string($xml);
foreach($configData->subscriptions->subscription as $subscription)
{
echo $subscription->getName();
}
You can use Xpath.
Please try below code, i have tested it with both of sample XML's you provided.
<?php
$subscriptions = simplexml_load_file('test.xml');
$scTag = $subscriptions->xpath("//subscription");
foreach($scTag as $subscription) {
echo $subscription->eventName;
/* using $subscription->eventName etc... */
}
?>
hope this help !

Related Links

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
simplexml_load_file not a valid URI issue
Can't turn XML response into an simplexml object
Having trouble getting Fogbugz API response into a SimpleXML object
Upgraded to php 5.3.10 and simpleXMLelement is broken
Parsing RSS Feed with SimpleXML
php simplexml_load_file (script url) failed to open stream
PHP - SimpleXML not returning object
Having trouble processing XML with PHP SimpleXML
Simplexml How to access all element of the same node
simplexml_load_file failed to open stream Http request failed
Using XML node names with hyphens in PHP [duplicate]

Categories

HOME
openlayers
atom-editor
debugging
keras
server
mediawiki
angular-material
smarty
lodash
filtering
cmd
fingerprint
django-imagekit
bs4
node-notifier
esper
medical
elasticsearch-hadoop
metatrader4
midi
wheelnav.js
modx-revolution
etl
zend-framework3
usergrid
seaborn
pythonanywhere
paging
icloud-api
rundeck
emgucv
pepper
cas
wtx
atlassian-crucible
flink-streaming
frame
uisplitview
ioio
copying
android-nestedscrollview
language-concepts
normal-distribution
host
plsql-psp
simplexml
lto
libvpx
android-browser
occlusion
twitch
az-application-insights
squib
preconditions
jvm-languages
midl
wso2carbon
auto-update
taffy
scrollable
dds
pnotify
hls.js
return-value
veracode
idisposable
snmptrapd
typed-lambda-calculus
wdf
azure-virtual-network
multi-level
websitepanel
angstrom-linux
hover-over
itextpdf
independentsoft
srand
fouc
embedded-code
nsight
cisco-ios
iiviewdeckcontroller
clipperlib
sorl-thumbnail
asp.net-web-api-odata
googlemock
ceil
dir
dexterity
apc
listings
jplaton
orchardcms-1.7
uiviewanimation-curve
postgresql-performance
f#-powerpack
oncheckedchanged
coda-slider
dentrix
modelstate
sef
xetex
yagni
mediarss

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