I want to handle a xml like this:
<GeocodeResponse>
<status>OK</status>
<result>
<type>street_address</type>
<address_component>
<long_name>Beijing</long_name>
<short_name>Beijing</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Beijing</long_name>
<short_name>Beijing</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
</result>
</GeocodeResponse>
what I wanna do is loop the node under < address_component >
when using this :
$doc = new DOMDocument();
$doc->loadXML($contents);
$addresses=$doc->getElementsByTagName("address_component");
foreach($addresses as $address){
$nodes = $address->$childNodes; //error arise here
for($i=0;$i<count($nodes);$i++){
//do work
}
}
It always show :
Fatal error: Cannot access empty property in C:\xampp\htdocs\read.php on line
$nodes = $address->$childNodes;
while it works fine with the code below :
$doc = new DOMDocument();
$doc->loadXML($contents);
$addresses=$doc->getElementsByTagName("address_component");
for($k=0;$k<$addresses->length;$k++){
$type_elements = $addresses->item($k)->getElementsByTagName('type');
//do work
}
I can not see the difference between these two method,in the first example,when I do the 'foreach' ,the $address I get is a single < address_component > element like this:
<address_component>
<long_name>Beijing</long_name>
<short_name>Beijing</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
right?
and of course it has $childNodes like < long_name >,< short_name >,etc.
but why I get empty property ?