XML Expat Library:
PHP's XML parser is based on Expat. It lets you parse but not validate XML documents means it does not allow us to find out the XML tags which are present instead of that we can check the correctness of the structure. PHP?s XML parser is event-based. In the following example you could learn about few in-built functions provided by PHP to access XML document.
Example:
<?php
$xml="<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?><lang><php>PHP is very easy to learn</php><Java>Java is the king of open source language</Java></lang>";
$parse=xml_parser_create();
xml_parse_into_struct($parse,$xml,$var1,$var2);
xml_parser_free($parse);
echo "<b>Index array:</b>"."<br/>";
print_r($var1);
echo "<br/><b>Vals array</b><br/>";
print_r($var2);
?>
Output:
Index array: Array ( [0] => Array ( [tag] => LANG [type] => open [level] => 1 ) [1] => Array ( [tag] => PHP [type] => complete [level] => 2 [value] => PHP is very easy to learn ) [2] => Array ( [tag] => JAVA [type] => complete [level] => 2 [value] => Java is the king of open source language ) [3] => Array ( [tag] => LANG [type] => close [level] => 1 ) ) Vals array Array ( [LANG] => Array ( [0] => 0 [1] => 3 ) [PHP] => Array ( [0] => 1 ) [JAVA] => Array ( [0] => 2 ) )