PHP DOMParser从XML标记获取选项名称和值

问题描述:


我想解析一个XML文件与PHP。
我使用这个代码,它可以很好地用于获取标记名和值:PHP DOMParser从XML标记获取选项名称和值

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

有人可以帮我找出我需要使用失控的optioname和optionvalue至极的选择?

所有答复都赞赏

这里是打印出来的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"; 
} 

希望这有助于。

+0

Thx!我正在寻找$ attr-> name和$ attr->值 – Tobrun 2012-03-22 14:33:31