2010-10-12 85 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