2012-08-02 25 views
1

我有一個XML解析器在我的PHP(simplexml的),並想選擇下的XPathXPath的選擇並保存特定的指數

function startXML(&$resarrary, $path, $lang) { 

if(file_exists($path)) { 
    $xml = simplexml_load_file($path); 
    if($xml) {  
     foreach($xml->xpath("//languageKey") as $cos) { 
     utf8_encode($cos); 

     // Select required languagekey 

     if ($cos[index] == $lang) { 
     foreach($xml->xpath("//languageKey[@index='$lang']//label[@type='js']") as $cos) { 
      ?> 
       <table> 
        <tr> 
         <td><?php echo $cos ?></td> 
        </tr> 
       </table>  
      <?php 
      array_push($resarrary,$cos); 

      } 

     } 

     } 

    } 
    else { 
     FALSE; 
    } 
} 

}

表保存索引元素回聲$ COS只是爲測試....它給了我從我的XML文件搜索正確的結果(在這種情況下,結果是「模式1」)

所以選擇工作,但我怎麼能保存結果(「模式1 「)到數組?如果我嘗試這array_push(...)並使用print_r查看resarray我只獲得simplexml xpath搜索的全部內容

Array([0] => SimpleXMLElement Object([@attributes] => Array( [index] => list_mode_1 [type] => js)[0] => Mode 1))

如果我改變了行array_push($ resarrary,$ cos-> attributes());我得到這個

陣列([0] => SimpleXMLElement對象([@屬性] =>數組([索引] => list_mode_1 [型] => JS)))

回答

0

XML

<lKey index="default" type="array"> 
    <a index="list_mode_1" type="js">Mode 1</a> 
    <b index="list_mode_2">Mode 2</b> 
    <c index="list_mode_3">Mode 3</c> 
</lKey>