使用字符串查询第二个XML页面

时间:2013-05-14 13:10:44

标签: php xml

是否有人愿意帮我弄清楚如何使用特许经营权ID从第二个XML页面中检索特许经营名称?

我正在处理两个不同的页面。第一页将为我提供特许经营权ID列表。我希望能够通过使用第一页中的特许经营权ID从第二个XML页面获取特许经营权名称。

如果您需要更多信息,请告诉我,以便能够提供帮助。

我正在寻找PHP的答案。

PHP:解析第一个XML页面

$url = "XMLPAGEURL";
 $xml = simplexml_load_file($url);
  foreach ($xml->franchise as $franchise) {
   echo ''.$franchise[id].'';
  }

第二个XML页面

<league id="1"/>
 <franchises count="5">
  <franchise name="A" id="0001"/>
  <franchise name="B" id="0002"/>
  <franchise name="C" id="0003"/>
  <franchise name="D" id="0004"/>
  <franchise name="E" id="0005"/>
 </franchises>
</league>

1 个答案:

答案 0 :(得分:1)

你不应该嵌套两组数据的循环,因为第二组中只有一个匹配,第一组的当前id。单独执行这些操作并合并数据是最佳解决方案

<?php
$data=array();

$url1 = "http://football99.myfantasyleague.com/2007/export?TYPE=standings&L=46184&XML=1";
$xml = simplexml_load_file($url1);
foreach ($xml->franchise as $franchise)
{
    $id=(string) $franchise->attributes()->id;

    $data[$id]['id']=$id;
    foreach($franchise as $key=>$value)
    {
        $data[$id][$key]=(string) $value;
    }
}

$url2 = "http://football99.myfantasyleague.com/2007/export?TYPE=league&L=46184&XML=1";
$xml_second = simplexml_load_file($url2);
foreach($xml_second->franchises->franchise as $franchise_sec)
{
    //var_dump($franchise_sec);
    $id=(string) $franchise_sec->attributes()->id;
    foreach($franchise_sec->attributes() as $key=>$value)
    {
        $data[$id][$key]=(string) $value;
    }
}

print_r($data);

这将id置于第一个循环中的数据中,name置于第二个循环中,使用id作为两者的索引(因为这是常见元素)
我也试图添加其他一些数据

更新,因为测试证明需要更改代码

生成的输出是: -

Array
(
    [0009] => Array
        (
            [id] => 0009
            [h2hw] => 12
            [name] => Hindenberg
            [logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0051.gif
        )

    [0004] => Array
        (
            [id] => 0004
            [h2hw] => 9
            [name] => Skins
            [logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0837.gif
        )

    [0010] => Array
        (
            [id] => 0010
            [h2hw] => 9
            [name] => Marooned
            [logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0841.gif
        )

    [0003] => Array
        (
            [id] => 0003
            [h2hw] => 7
            [name] => Elf Boys
            [logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0703.gif
        )

    [0007] => Array
        (
            [id] => 0007
            [h2hw] => 7
            [name] => Juggernaut
            [logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0689.gif
        )

    [0006] => Array
        (
            [id] => 0006
            [h2hw] => 7
            [name] => Knights
            [logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0708.gif
        )

    [0002] => Array
        (
            [id] => 0002
            [h2hw] => 7
            [name] => Dr. Death
            [logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0829.gif
        )

    [0005] => Array
        (
            [id] => 0005
            [h2hw] => 6
            [name] => Busted Season
            [logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0842.gif
        )

    [0001] => Array
        (
            [id] => 0001
            [h2hw] => 4
            [name] => Deep Divot
            [logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0826.gif
        )

    [0008] => Array
        (
            [id] => 0008
            [h2hw] => 3
            [name] => Last Place
            [logo] => http://www99.myfantasyleague.com/fflnet2005/helmets/h-0827.gif
        )

)

您可以按如下方式检索数据: -

while(list($key,$franchise)=each($data))
{
    echo $franchise['id'].': '.$franchise['name'].' <img src="'.$franchise['logo'].'"/><br />';
}