I have this XML:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<header>
<export_time>2012-08-28 08:13:36</export_time>
<file_name>nameasd</file_name>
</header>
<shipping>
<shipping_number>some data</shipping_number>
<location_id>some data</location_id>
<status>DONE</status>
<shipping_line>
<article id="1257" />
<article id="1177" >
<product>5500070000126273</product>
<product>5500070000126264</product>
<product>5500070000126255</product>
<product>5500070000126246</product>
<product>5500070000126237</product>
</article>
</shipping_line>
</shipping>
</data>
Ii can access data like this:
$shipping_number_array = $xml->xpath('/data/shipping/shipping_number');
$location_id_array = $xml->xpath('/data/shipping/location_id');
$shipping_status_array = $xml->xpath('/data/shipping/status');
$shipping_number = $shipping_number_array[0];
$location_id = $location_id_array[0];
$status = $shipping_status_array[0];
Now I would like to check if the article element has any children and if yes then put them in an array.
This doesn’t seem to work, I get error : call to a member function hasChildren() on a non-object.
if ($article_array->hasChildren()) {
error_log('has children');
}
You can use xpath’s
child::location path to select the article node childs and back up one for the article node like this:This should only return the article nodes that has childs (with id 1177 in your example).