0

I have the following XML file:

<XmlSports xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" CreateDate="2014-11-17T14:46:38.6954+01:00">
  <Sport Name="SOCCER" ID="7">
    <Event Name="African Nations Cup" ID="54" IsLive="false">
      <Match Name="DR Congo v Sierra Leone" ID="1552163" StartDate="2014-11-19T15:00:00.1+01:00">
        <Bet Name="Double Chance" ID="6337125"/>
        <Bet Name="Draw No Bet" ID="6337124"/>
        <Bet Name="Match Odds" ID="6337119"/>
        <Bet Name="Odds or Evens" ID="6337123"/>
        <Bet Name="Over/Under" ID="6337121"/>
      </Match>
    </Event>
  </Sport>

I am just including a small portion of the file but basically this file includes other sports aside from just soccer, separated by 'Sport Name="NAME OF SPORT" ID="ID OF SPORT"'.

I want to extract just the SOCCER node. I have tried using XPath but have not succeeded. How can I extract just the 'Sport Name="SOCCER" ID="7"' part out of this XML?

moopet
  • 6,014
  • 1
  • 29
  • 36
cana
  • 3
  • 1
  • 1
  • I do not know how to accomplish this with dom either. Could you please shed a light? Whatever I try I just cannot succeed to accomplish the extracting of just the Soccer node – cana Nov 17 '14 at 15:11
  • http://stackoverflow.com/questions/13194183/how-to-get-attribute-value-from-node-using-xpath – Marc B Nov 17 '14 at 15:12
  • I am still clueless as xpath is what I have already tried and failed. Could you please show me the correct syntax to extract the part I am looking after. I'd appreciate that a lot. – cana Nov 17 '14 at 15:29

2 Answers2

0

This should give you a starting point. Assuming that $string contains your valid xml.

$xml = simplexml_load_string($string);
$sport = $xml->xpath("//Sport[@Name='SOCCER']");
print_r($sport);
Jens W
  • 188
  • 6
  • Thanks for helping me out. Unfortunately your code also gives me the other sports just like my own attempt. If it helps, here is the feed url I am using: http://odinbet.com/sportsxml. You can see that it contains other sports aside from soccer and I am trying to isolate just the soccer parts. – cana Nov 17 '14 at 15:38
  • Are you sure? I get the Node and all the containing nodes but not the sibblings. e.g my result contains no node with "American Football".. – Jens W Nov 17 '14 at 15:48
  • My bad, you are absolutely right. Thanks a lot for your effort. I'll rate your answer. Thanks again. – cana Nov 17 '14 at 15:53
0

I wish you have tried this one

//XmlSports/Sport[@Name='SOCCER' and @ID='7']
Vinod Srivastav
  • 3,644
  • 1
  • 27
  • 40