I have many xml file’s, I do not want to get the value, but check if they have a child child nod or no. so I use count instead of foreach. but my code always echo no if there have no $xml->book->date in the first $xml->book roop.
$xml = simplexml_load_file('some.xml');
if (@count($xml->book->date)>0){
echo 'yes';
}else{
echo 'no';
}
the xml tree like this
<?xml version="1.0" encoding="utf-8"?>
<Catalog>
<book>
<title/><description/><price/>
</book>
<book>
<title/><description/><price/>
<date/> //(optional, there may be 0 to n [book] elements having a [date] element in the document)
</book>
<book>
<title/><description/><price/>
</book>
<book>
<title/><description/><price/>
<date/>
</book>
</Catalog>
For this document the result of the working version of “count($xml->book->date)” should be 2, i.e. > 0
EDIT: maybe do not use count, but how to check if have or not have date childnod in the whole xml file? Thanks.
If you want to check in the entire xml document, then I think you’ll have to loop through all the book elements-