PHP - SimpleXMLIterator::valid() Function
Definition and Usage
XML is a mark-up language to share the data across the web, XML is for both human read-able and machine read-able. The SimpleXMLIterator is used to iterate all the nodes of an XML document.
The
SimpleXMLIterator:: valid()
function is used to verify whether the current element of an iterator is valid.Syntax
SimpleXMLIterator::valid(void);
Parameters
This function doesnt accept any parameters.
Return Values
This function returns a boolean value which is TRUE if the current element is valid and, FALSE if not.
PHP Version
This function was first introduced in PHP Version 5 and works in all the later versions.
Example
Following example demonstrates the usage of the SimpleXMLIterator::valid() function.
<html>
<head>
<body>
<?php
$doc = new DOMDocument;
$data = "<Test><Name>Raju</Name></Test>";
//Creating the SimpleXMLIterator
$xmlIterator = new SimpleXMLIterator($data);
$xmlIterator->rewind();
$res = current($xmlIterator);
print_r($res. "<br>");
$isValid = $xmlIterator->valid();
if($isValid){
print("Given element is valid");
} else {
print("Given element is not valid");
}
?>
</body>
</head>
</html>
This will produce following result −
Raju Given element is valid
Example
Following is another example of this function −
Data.xml:
<Tutorial> <Name>Krishna</Name> <Pages>30</Pages> </Tutorial>
sample.html
<html>
<head>
<body>
<?php
$doc = new DOMDocument;
//Creating the SimpleXMLIterator
$xmlIterator = new SimpleXMLIterator("data.xml", 0, TRUE, "", FALSE);
$xmlIterator->rewind();
if($xmlIterator->valid()){
print_r($xmlIterator->current());
echo "<br><br>";
$xmlIterator->next();
print_r($xmlIterator->current());
} else {
print("Sorry");
}
?>
</body>
</head>
</html>
This will produce the following output −
SimpleXMLIterator Object ( [0] => Krishna ) SimpleXMLIterator Object ( [0] => 30 )
php_function_reference.htm
Advertisements