How to Check if a Node Is Empty in XML
PHP throws an error when you try to process an XML node that contains a Null value. You must first check if the variable contains an empty value before using the XML code in your calculations. PHP has an Empty function that tells you if the retrieved XML node does not contain a value. You contain any calculations within the conditional statement to avoid errors in your PHP pages.
Instructions
-
-
1
Right-click the PHP page that imports your XML data. Click "Open With," then click the PHP editor you use to edit your Web pages.
-
2
Add the code to search for the particular XML element. The following code gets the value of an element with the ID "customerid_5":
$element = new searchElementById("customerid_5", $children);
$found = $element->beginSearching(); -
-
3
Detect if the element has no value. Using the Empty function, the following code displays "No data in the element" if the element has no value:
if(empty($found)) {
echo "No data in the element";
}
-
1