In spite of the growing popularity of XML for storing and exchanging data of nearly any kind imaginable, XML is not well suited to act as a direct replacement for some of its defined subsets or sublanguages, like HTML. |
This is because XML defines only a standard for structuring data XML itself fails (indeed, by design) to provide any standard for how XML data in the general case should be rendered or displayed to the user. |
Such concerns, particularly in the case of the World Wide Web and the documents that it contains, are the domain of XML-compliant document type definitions such as Hypertext Markup Language (HTML) or Extensible Hypertext Markup Language (XHTML). |
Displaying and rendering standards like XHTML govern the ways in which the data and tags that form the structure of compliant XML documents are actually rendered onscreen for readers or World Wide Web users. |
Parsing XML data with SimpleXML |
What is SimpleXML? |
When people ask me “What is SimpleXML?” I often quip, “XML is the solution to all your problems; SimpleXML ensures it isn’t the root of your problems!” [Andres P. Ferrando] |
SimpleXML is new in PHP 5. It is an easy way of getting an element’s attributes and text, if you know the XML document’s layout. Compared to DOM or the Expat parser, SimpleXML just takes a few lines of code to read text data from an element. |
SimpleXML converts the XML document into an object, like this: |
• Elements – Are converted to single attributes of the SimpleXMLElement object. When there’s more than one element on one level, they’re placed inside an array |
• Attributes – Are accessed using associative arrays, where an index corresponds to the attribute name |
• Element Data – Text data from elements are converted to strings. If an element has more than one text node, they will be arranged in the order they are found |
SimpleXML is fast and easy to use when performing basic tasks like: |
• Reading XML files • Extracting data from XML strings • Editing text nodes or attributes |
However, when dealing with advanced XML, like namespaces, you are better off using the Expat parser or the XML DOM. |
Note: SimpleXML Parser is available only with PHP 5 and above. |
Example: |
simpleXML.php |
When using SimpleXML parser to parse XML files, |
simplexml_load_file(file_path) method is used to load the XML file. |
$emp=simplexml_load_file(“employee.xml”) or die(“Unable to Load XML file”); |
The example given below loads the XML file employee.xml and displays the data of the XML file in Table format: |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title> <?php echo "PHP Tutorial: Working With SimpleXML Parser";?></title> </head> <body> <?php $emp=simplexml_load_file("employee.xml") or die("Unable to Load XML file"); $tbl=<<<TBL <table border="1" cellspacing="1"> <tr bgcolor="blue"> <td> Employee ID</td> <td> Name </td> <td> Age</td> <td> Department </td> <td> Designation</td> <td> Phone No</td> <td> Address</td> </tr> TBL; echo $tbl; if($emp) { foreach($emp->employee as $ebiz) { foreach($ebiz->employee_details as $employee_details) { echo "<tr> \n<td>".$employee_details->emp_id."</td>\n"; echo "<td>".$employee_details->fname." ".$employee_details->lname."</td>\n"; echo "<td>".$employee_details->age."</td>\n"; echo "<td>".$employee_details->department."</td>\n"; echo "<td>".$employee_details->designation."</td>\n"; echo "<td>".$employee_details->phone."</td>\n"; echo "<td>".$employee_details->address."</td>\n"; } } } ?> </body> </html> |
Output: |
![]() |