Php SimpleXML checks if a child exists

A->b->c may exist, but c may not exist. How to check it?

+53
php simplexml
Oct 13 '09 at 15:04
source share
15 answers
 if($A->b->c != null) //c exists 

If c does not exist, its value will be null (or, more precisely, it will not matter). Note, however, that for this, both A and b need not be null . Otherwise, PHP will throw an error (I think).

-13
Oct 13 '09 at 15:17
source share

Better wrap this in isset()

 if(isset($A->b->c)) { // c exists 

Thus, if $A or $A->b does not exist ... it will not explode.

+112
Oct 13 '09 at 18:41
source share

SimpleXML always returns an object. If there is no child, an empty object is returned.

 if( !empty($a->b)){ var_dump($a->b); } 
+39
Mar 17 2018-11-11T00:
source share

I solved it using the children() function and ran count() on it, ignoring the PHP error if there are no children, putting @ before the call to count. This is stupid, but it works:

 $identification = $xml->identification; if (@count($identification->children()) == 0) $identification = $xml->Identification; 

I hate this...

+7
Nov 24 '10 at 15:11
source share

Xpath method returns an array of matched elements or false

 if(false !== $A->xpath('b/c')) { ... 

http://www.php.net/manual/ru/simplexmlelement.xpath.php

+5
Sep 25 '13 at 10:32
source share

After some experimentation, I found that the only reliable method for checking the existence of a node is using count($xml->someNode) .

Here is a test case: https://gist.github.com/Thinkscape/6262156

+5
Apr 10 '14 at 12:52
source share

If you have PHP 5.3, you can simply use $a->count() . Otherwise, the scippie solution using @count($a->children()) works well. I believe I do not need @, but an earlier PHP implementation may be required.

+4
Mar 23 2018-11-23T00:
source share

Using if(isset($A->b){ gave me problems, so I tried if($A->b){ and it worked!

+2
Jun 06 2018-12-12T00:
source share

Using xpath:

 function has_child(\SimpleXMLElement $parent=null, string $xpathToChild) { return isset($parent) && !empty($parent->xpath('('.$xpathToChild.')[1]')); } 

where $parent is the indirect or direct parent of the child being checked, and $xpathToChild is the xpath of the child relative to $parent .

()[1] because we do not want to select all child nodes. One is enough.

To check if $ a-> b-> c exists:

has_child($a,'b/c');

You can also check attributes. To check if node c attribute t .

 has_child($a,'b/c/@t'); 
+2
Apr 18 '17 at 13:09 on
source share

The 3 ways to confirm work in PHP 5.5.23 were to use isset() count() or empty()

Here is a script to show the results from each:

https://gist.github.com/mchelen/306f4f31f21c02cb0c24

+1
Apr 15 '15 at 18:43
source share

Simply

 var_dump(count($xml->node)); 
+1
Mar 15 '17 at 18:04
source share

I use a helper function to check if the node is a valid node provided as a parameter in the function.

 private static function isValidNode($node) { return isset($node) && $node instanceof SimpleXMLElement && !empty($node); } 

Usage example:

 public function getIdFromNode($node) { if (!self::isValidNode($node)) { return 0; } return (int)$node['id']; } 
+1
Jun 7 '18 at 13:27
source share

You can try:

 if($A->b->c && $A->b->c != '') 
0
Jun 21 '11 at 16:08
source share

I thought I would share my experience. Running on 5.4. I tried testing with "isset" and "empty", but it didn’t work for me. I ended up using is_null .

 if(!is_null($xml->scheduler->outterList->innerList)) { //do something } 
0
Aug 02 '13 at
source share

Namespaces

Remember that if you use namespaces in your XML file, you will need to include them in function calls when checking for children, otherwise it will return ZERO every time:

 if ($XMLelement->children($nameSpace,TRUE)->count()){ //do something here } 
0
01 Oct '14 at 17:26
source share



All Articles