0

以下にこのビットのxmlコードがあり、resultsタグ内のすべての「resultvalue」属性の値を取得しようとしています。問題は...これはライブフィードになるため、そのタグ内に1、2、または3つの結果アイテムが含まれる可能性があります。

結果タグ内にあるアイテムの数を確認するために、なんらかのカウントを行う必要がありますか?

<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>

前もって感謝します

4

1 に答える 1

1

SimpleXML

SimpleXMLで結果をループして、それぞれvaluename属性を取得するだけです。これは、可変数の結果で機能します。

デモ

$obj = simplexml_load_string($xml);

foreach($obj->Results->Result as $result)
{
    echo $result->attributes()->name . ': ' . $result->attributes()->value . "\n";
}

出力

電流:1-1
FT:1-1
HT:1-0

Matchesその下に複数あるようなルートノードがある場合は、次のようMatchにネストされたものを使用します。foreach

foreach($obj->Match as $match)
{
    foreach($match->Results->Result as $result)
    {
        echo $result->attributes()->name . ': ' . $result->attributes()->value . "\n";
    }
}

DOMDocument

SimpleXMLの代わりにDOMDocumentを使用して同じことを行うには:

$dom = new DOMDocument();
$dom->loadXML($xml);

foreach($dom->getElementsByTagName('Match') as $match)
{
    foreach($match->getElementsByTagName('Result') as $result)
    {
        echo $result->getAttribute('name') . ': ' . $result->getAttribute('value') . "\n";
    }
}

SimpleXMLメソッドと同じように出力します。

于 2012-12-11T14:58:12.747 に答える