I have this bit of xml code below and I’m trying to get the value of all of the “result value” attributes within the results tag. The thing is…this is going to be a live feed, so there may be 1,2 or 3 result items within that tag.
Do I need to do some sort of count to see how many items are within the results tag?
<Match ct="0" id="771597" LastPeriod="2 HF" LeagueCode="19984" LeagueSort="1" LeagueType="LEAGUE" startTime="15:00" status="2 HF" statustype="live" type="2" visible="1">
<Home id="11676" name="Manchester City" standing="1"/>
<Away id="10826" name="Newcastle United" standing="3"/>
<Results>
<Result id="1" name="CURRENT" value="1-1"/>
<Result id="2" name="FT" value="1-1"/>
<Result id="3" name="HT" value="1-0"/>
</Results>
<Information>
<league id="19984">Premier League</league>
<note/>
<bitarray/>
<timestamp/>
</Information>
</Match>
Thanks in advance
SimpleXML
Just loop through the results with SimpleXML to grab each
valueandnameattribute, this will work with a variable number of results.Demo
Outputs
If you have a root node such as
Matcheswith multipleMatchunder it then you would use a nestedforeachlike so:DOMDocument
To do the same using DOMDocument instead of SimpleXML:
Outputs the same as the SimpleXML method.