I have some xml data in the format below -
<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>
I do something like
$xml = simplexml_load_string($xml_data);
$namespaces = $xml->getNameSpaces(true);
$data = $xml->children($namespaces['xyz']);
so if I print
echo $data->totalRecords; //outputs correctly 659
I am trying to loop through all of the records and access individual fields but have no clue how to do that.
开发者_开发问答I tried something like
$records = $data->records;
foreach($records as $record) {
echo print_r($record, true);
}
but it wasnt very helpful.
My questions are -
- how to access each <xyz:record>
and its sub items (which are <str name="abc">NAbc</str>
etc?
With thanks, Kay
I would use the xpath() method:
$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
}
}
(Untested.)
When you select $data->records
you select all records
elements, not the record
, I think you should use $data->records->record
to iter all record
elements.
$records = $data->records->record;
foreach($records as $record){
$doc = $record->doc;
//Now here you can access all str elements itering through $doc->str elements.
}
Here is PHP example: SimpleXMLElement
Update (As we say in Italy "You never stop to learn")
You can access xml elements as objects only if they share the same namespace, you can do this:
$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
Just for the record.
精彩评论