simplexml


Simplexml How to access all element of the same node


[Villa] => Array
(
[0] => SimpleXMLElement Object
(
[VillaID] => 6
[VillaName] => Mary
[Distances] => SimpleXMLElement Object
(
[Distance] => Array
(
[0] => SimpleXMLElement Object
(
[Destination] => Sea
[Value] => 1000 m
)
[1] => SimpleXMLElement Object
(
[Destination] => Market
[Value] => 800 m
)
)
)
)
[1] => SimpleXMLElement Object
(
[VillaID] => 21
[VillaName] => Marion
[Distances] => SimpleXMLElement Object
(
[Distance] => Array
(
[0] => SimpleXMLElement Object
(
[Destination] => Beach
[Value] => 5 min
)
)
)
)
)
I need to print all, only of 1 villa (example with id = 6) but VillaId is not an array so it's impossible to get all with foreach
I can obtain it with:
echo 'Name of Villa: '.$xml->Villa[0]->VillaName.'';
etc. etc ... but in this way have to change manually for every villa (too much) the value in the brackets.
i've tried with $xml->Villa[$value]->VillaName; ($value comes from another page) but it's not working...
Tanks for help!
First of all, your question starts with "i have this xml:" followed by something that is not XML. I'm not saying this to be a smartass, rather because it's important for XML beginners to understand that print_r() is not the right way to inspect SimpleXMLElements. Sometimes it will show you things that aren't in your XML, other times it will not show things that are actually in your XML. In short: do not use print_r() on SimpleXMLElement. Just use ->asXML() and look at the actual XML.
From what I understand, you want to locate and select a node based on some criteria. XML just happens to have a language for that: XPath. The official specs aren't terribly user-friendly but w3schools.com has a pretty good XPath tutorial.
I need to print all, only of 1 villa (example with id = 6) but VillaId is not an array so it's impossible to get all with foreach
Anywhere in your document, you want to select all Villa nodes with an attribute VillaID whose value is "6". In XPath:
//Villa[#VillaID="6"]
Via SimpleXML:
$xml->xpath('//Villa[#VillaID="6"]');
Attention, xpath() always return an array.

Related Links

SimpleXMLElement::__construct(): Entity: line 1: parser error : Start tag expected, '<' not found
Add xml attribute documentname
Pick shortest route with simpleXML
SimpleXML extract text between two tags
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

Categories

HOME
push-notification
view
session
google-docs
programming-languages
qore
cloudkit
facebook-php-sdk
izpack
quickbooks
qt-creator
messages
ibm-odm
designer
serverless-framework
django-admin
facebook-page
nodatime
visjs
xlsxwriter
riot.js
physics-engine
telerik-reporting
bootstrap-tour
captiveportal
numerical-methods
buildbot
kannel
brunch
filezilla
fifo
delicious-api
tinymce-4
amazon-kinesis-kpl
wixsharp
git-merge
adobe-premiere
qwerty
multilingual
libvpx
az-application-insights
grid.mvc
elmah
http-live-streaming
arena-simulation
bootstrap-dialog
outlook-api
http-redirect
parentheses
pubmed
blogengine.net
tizen-native-app
svmlight
statsd
memory-alignment
static-ip-address
probability-density
bonobo
firebaseui
gstreamer-0.10
clang-static-analyzer
hill-climbing
hover-over
whois
pickadate
dlna
yaws
suffix-tree
revolution-r
browser-link
javax.sound.midi
ios8-today-widget
jqgrid-formatter
computer-algebra-systems
emailrelay
splash
sysinternals
graph-drawing
sortedlist
notify
npapi
clicktag
android-nested-fragment
mdt
markers
issuu
project-planning
starcluster
stxxl
odata4j
gwt-rpc
eventual-consistency
django-nonrel
chuck
venn-diagram
tridion-worldserver
amazon-appstore
stage
vdsp
joyent
yui-datatable
fluent-interface
sudzc
zend-tool
virtual-functions
spec#
private-members

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App