xml file read

XmlFileExample.php

<!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>xml file read example in php</title>
</head>

<body>
 <h2 style="padding-top:0px; margin-top:0px;">xml file read example in php</h2>
 <h3>Patients.xml</h3>
  <?xml version="1.0"?>
  <patients> 
   <patient>
     <age>30</age>
     <name>cfsuman</name>     
     <address>mohakhali</address>
   </patient>
   <patient>
     <age>300</age>
     <name>modon</name>     
     <address>Earth</address>
   </patient>
   <patient>
     <age>28</age>
     <name>iqbal</name>     
     <address>uttara</address>
   </patient>
  </patients>
 <?php
  /*
  |--------------------------------------------------
  | create a file named Patients.xml &
  | cut the above code and paste it.
  |--------------------------------------------------
  */
  $myXml = simplexml_load_file("Patients.xml") or die("Error: Cannot create object");
  /*
  |--------------------------------------------------
  | Interprets the xml file to an object using 
  | simplexml_load_file() & assign to a variable.
  |--------------------------------------------------
  */
  echo "<pre>";
  print_r($myXml);
  echo "</pre>";
  /*
  |----------------------------------------------------------
  | $myXml is a converted object and we can access like this :
  |----------------------------------------------------------
  */
 ?>
 <div style="height:20px; background:#aaa; color:pink; font-weight:bold; padding:5px; margin-bottom:1px;">
  <?=$myXml->patient[0]->age;?>
 </div>
 <div style="height:30px; background:#aab; color:orange; font-weight:bold; padding:5px; margin-bottom:1px;">
  <?=$myXml->patient[1]->address;?>
 </div>
 <div style="height:30px; background:#aaa; color:pink; font-weight:bold; padding:5px;">
  <?=$myXml->patient[2]->name;?>
 </div>
</body>
</html>



xml file read example in php output


No comments:

Post a Comment

leave your comments here..