2010-10-12 48 views
1

我在下面的格式某些XML數據 -如何檢索XML數據的孩子?

<xyz:reqResponse 
    xmlns:xyz="http://www.test.com/xyz/" 
    xmlns:akt="http://www.exmple.com/akt/"> 
    <xyz:version>1.2</xyz:version> 
    <xyz:totalRecords>659</xyz:totalRecords> 
    <xyz:records> 
    <xyz:record> 
     <doc> 
      <str name="icon_url">http://www.icons.net/icon1.jpg</str> 
      <str name="type">Service</str> 
      <arr name="bc.recordTitle"><str>Evergreen Club</str></arr> 
      <str name="bc.id">KLM0078</str> 
      <str name="system.external.id">787678</str> 
      <arr name="bc.description"> 
       <str>Meetings: Church Hall Beaudesert Lane. Open to anyone over 50.</str> 
      </arr> 
      <str name="code">X1209</str> 
      <double name="localval1">-4.00006</double> 
      <double name="localval2">-7.00012</double> 
      <date name="timestamp">Wed Jun 02 21:19:33 BST 2010</date> 
     </doc> 
    </xyz:record> 
    <xyz:record> 
     <doc> 
      <str name="icon_url">http://www.icons.net/icon1.jpg</str> 
      <str name="type">Service</str> 
      <arr name="bc.recordTitle"><str>Evergreen Club</str></arr> 
      <str name="bc.id">KLM0078</str> 
      <str name="system.external.id">787678</str> 
      <arr name="bc.description"> 
       <str>Meetings: Church Hall Beaudesert Lane. Open to anyone over 50.</str> 
      </arr> 
      <str name="code">X1209</str> 
      <double name="localval1">-4.00006</double> 
      <double name="localval2">-7.00012</double> 
      <date name="timestamp">Wed Jun 02 21:19:33 BST 2010</date> 
     </doc> 
    </xyz:record> 
    </xyz:records> 
</xyz:reqResponse> 

我做這樣的事情

$xml = simplexml_load_string($xml_data);    
$namespaces = $xml->getNameSpaces(true); 
$data = $xml->children($namespaces['xyz']); 

,所以如果我打印

echo $data->totalRecords; //outputs correctly 659 

我通過所有的努力環記錄和訪問個別領域,但不知道如何做到這一點。

我想是這樣

$records = $data->records; 
foreach($records as $record) { 
    echo print_r($record, true); 
} 

,但它不是非常有幫助。

我的問題是 - - 如何訪問每個<xyz:record>及其分項(這是<str name="abc">NAbc</str>

與感謝, 凱

+0

「它不是非常有幫助」 - 你能告訴我們它打印的內容(未經測試)? – LarsH 2010-10-12 16:36:36

+0

@LarsH ..它俘虜這些.. SimpleXMLElement對象([記錄] =>數組([0] => SimpleXMLElement對象()[1] => SimpleXMLElement對象()))..空的那麼沒有幫助 – Kay 2010-10-12 16:57:10

回答

2

我會用xpath()方法:

$xml = simplexml_load_string($xml_data); 

$xml->registerXPathNamespace('xyz', 'http://www.test.com/xyz/'); 

$recordArr = $xml->xpath('/xyz:reqResponse/xyz:records/xyz:record'); 

foreach($recordArr as $record) { 
    $strArr = $record->xpath('doc/str'); 

    foreach($strArr as $str) { 
     do what you want with $str 
    } 
} 

+0

謝謝按預期工作。乾杯! – Kay 2010-10-13 08:54:57

1

當您選擇$data->records您選擇所有records元素,而不是? record,我認爲你應該使用$data->records->record到ITER所有record元素。

$records = $data->records->record; 
foreach($records as $record){ 
    $doc = $record->doc; 
    //Now here you can access all str elements itering through $doc->str elements. 
} 

這裏是PHP例子:SimpleXMLElement

更新(如我們在意大利說:「你永遠不會停止學習」)

您可以訪問XML元素作爲對象只有當它們共享相同的命名空間,你可以做這個:

$docsCollection = $record->children(); //This will retrieve children within default namespace 
$strCollection = $docs->doc->str; //Now we are in the same namespace and can reuse above notation 

只是備案。

+0

好抓,關於''元素與''元素。 – LarsH 2010-10-12 19:52:43

+0

遺憾的上述XML數據它只信息 - SimpleXMLElement對象 ( ) SimpleXMLElement對象 ( ) – Kay 2010-10-13 08:58:53

+0

注意到。看來我在處理命名空間時遇到了一些問題。 – Minkiele 2010-10-13 10:27:21