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

PHP 7 simpleXML fatal error
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

Categories

HOME
osgi
gerrit
activiti
minimum-spanning-tree
fluentd
pycharm
kde
fme
grep
getelementsbytagname
amazon-ecs
win32gui
ravendb
serverspec
enterprise-library-5
autotools
basic
gorm
node-pdfkit
google-apps-marketplace
netflix
lombok
uitypeeditor
transformation
alpine
paging
ml
intentfilter
accessor
devops
fish
virtualdub
bootstrap-material-design
fifo
google-rich-snippets
javascriptcore
pdb
repo
vxworks
jquery-bootgrid
streamsets
c11
theano.scan
unobtrusive-validation
ws-security
android-tabhost
avro4s
ncalc
sql-server-agent
estimote
midl
vao
http-redirect
.net-4.6.2
google-datalayer
taffy
appcompat
pubmed
dds
boost-multi-index
drupal-6
diagnostics
sqlbulkcopy
visual-c++-2008
static-ip-address
passport-google-oauth
removechild
radtreelist
py2app
ctest
packagist
oauth2client
msys
python-3.2
tableau-online
splash
createprocessasuser
system32
unity5.2.3
dereference
javax.mail
system.reflection
oxwall
ng-animate
angularjs-ng-click
asp.net-dynamic-data
device-manager
rebol3
balanced-payments
message-driven-bean
html-editor
bluepill
datawindow
qtkit
code-cleanup
bigcouch
nsdatecomponents
doh
xmlspy
jmock
gallio
sef
xetex

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