2


PHPでxmlファイルを解析しようとしています。
私はこのコードを使用していますが、タグ名と値を取得するのにうまく機能します:

function getChildNodes($dom,$SearchKey){

  foreach ($dom->getElementsByTagName($SearchKey) as $telefon) {
      foreach($telefon->childNodes as $node) {
          print_r(
          $SearchKey . " : " . $node->nodeValue . "\n"                
      );
  }

} }

コードの作業部分の xml の例:

<inputs>
 <input>C:\Program Files\VideoLAN\VLC\airbag.mp3</input>
 <input>C:\Program Files\VideoLAN\VLC\sunpark.mp3</input>
 <input>C:\Program Files\VideoLAN\VLC\rapidarc.mp3</input>
</inputs>

動作しないコードの xml の例:

<instances>
 <instance name="default" state="playing" position="0.050015" time="9290569" length="186489519" rate="1.000000" title="0" chapter="0" can-seek="1" playlistindex="3"/>
</instances>

オプション名とオプション値を取得するために使用する必要があるオプションを理解するのを手伝ってくれる人はいますか?

すべての応答は高く評価されます

4

1 に答える 1

1

XML 属性を出力するためのコード サンプルを次に示します。

<?php

$source = '<instances><instance name="default" state="playing" position="0.050015" time="9290569" length="186489519" rate="1.000000" title="0" chapter="0" can-seek="1" playlistindex="3"/></instances>';

$doc = new DOMDocument();
$doc->loadXML($source);

$el = $doc->firstChild->firstChild;

for ($i = 0; $i < $el->attributes->length; $i++) {
    $attr = $el->attributes->item($i);
    echo 'Name: '.$attr->name, "\n";
    echo 'Value: '.$attr->value, "\n";
}

お役に立てれば。

于 2012-03-22T14:26:28.387 に答える