0

Here is a sample of the xml File. If I have the id value of 20 which is halfway through the xml file. How do I first search that node out and second find the next (following) value.

<League>
  <Id>20</Id>
  <Name>Major League Soccer</Name>
  <Country>USA</Country>
  <Historical_Data>Partial</Historical_Data>
  <Fixtures>Yes</Fixtures>
  <Livescore>Yes</Livescore>
  <NumberOfMatches>135</NumberOfMatches>
  <LatestMatch>2013-06-16T04:00:00+02:00</LatestMatch>
</League>
<League>
  <Id>33</Id>
  <Name>Allsvenskan</Name>
  <Country>Sweden</Country>
  <Historical_Data>Partial</Historical_Data>
  <Fixtures>Yes</Fixtures>
  <Livescore>Yes</Livescore>
  <NumberOfMatches>88</NumberOfMatches>
  <LatestMatch>2013-06-15T16:00:00+02:00</LatestMatch>
</League>
hakre
  • 193,403
  • 52
  • 435
  • 836
Somk
  • 11,869
  • 32
  • 97
  • 143
  • It's also good if you share a little snippet what you've done so far. In this case for example the xpath you've tried and also the PHP chunk where you try it. You will get more exemplary answers then and also the person who answers will be able to explain you better where she spots your issue. – hakre Jun 20 '13 at 07:20
  • This might be only one part of the problem solution, for the other part, you need to query based on content. That has been outlined as well, for example in (which looks like even a better duplicate): [PHP xpath - find element with a value and also get elements before and after element](http://stackoverflow.com/q/6279544/367456) – hakre Jun 20 '13 at 07:38

1 Answers1

1

Since League with id = 20 is halfway through the XML file you can do it dynamically using XPath. Here's a simple PHP code snippet:

<?php
$xml = <<<XML
<Leagues>
    <League>
        <Id>20</Id>
        <Name>Major League Soccer</Name>
        <Country>USA</Country>
        <Historical_Data>Partial</Historical_Data>
        <Fixtures>Yes</Fixtures>
        <Livescore>Yes</Livescore>
        <NumberOfMatches>135</NumberOfMatches>
        <LatestMatch>2013-06-16T04:00:00+02:00</LatestMatch>
    </League>
    <League>
        <Id>33</Id>
        <Name>Allsvenskan</Name>
        <Country>Sweden</Country>
        <Historical_Data>Partial</Historical_Data>
        <Fixtures>Yes</Fixtures>
        <Livescore>Yes</Livescore>
        <NumberOfMatches>88</NumberOfMatches>
        <LatestMatch>2013-06-15T16:00:00+02:00</LatestMatch>
    </League>
</Leagues>
XML;

$sxe = new SimpleXMLElement($xml);

// Retrieve league with Id = 20
$league20 = $sxe->xpath("//League[Id='20']");
print_r($league20);

// Retrieve league right after the one with Id = 20
$leagueAfter20 = $sxe->xpath("//League[Id='20']/following-sibling::League[1]");
print_r($leagueAfter20);

Output

Array
(
[0] => SimpleXMLElement Object
    (
        [Id] => 20
        [Name] => Major League Soccer
        [Country] => USA
        [Historical_Data] => Partial
        [Fixtures] => Yes
        [Livescore] => Yes
        [NumberOfMatches] => 135
        [LatestMatch] => 2013-06-16T04:00:00+02:00
    )
)
Array
(
[0] => SimpleXMLElement Object
    (
        [Id] => 33
        [Name] => Allsvenskan
        [Country] => Sweden
        [Historical_Data] => Partial
        [Fixtures] => Yes
        [Livescore] => Yes
        [NumberOfMatches] => 88
        [LatestMatch] => 2013-06-15T16:00:00+02:00
    )
)
Rolando Isidoro
  • 4,983
  • 2
  • 31
  • 43