xml node read

XmlNodeRead.php
patients2.xml

<?xml version="1.0"?>
<documents>
 <hospital>
  <patients> 
   <patient>
    <age>32</age>
    <name>hannan</name>     
    <address>gulshan</address>
   </patient>
   <patient>
    <age>28</age>
    <name>iqbal</name>     
    <address>uttara</address>
   </patient>
  </patients>
 </hospital>
 <hospital>
  <patients> 
   <patient>
    <age>25</age>
    <name>monalisa</name>     
    <address>torronto</address>
   </patient>
   <patient>
    <age>24</age>
    <name>ripa</name>     
    <address>halishohor</address>
   </patient>
   <patient>
    <age>23</age>
    <name>rupa</name>     
    <address>chalkbazar</address>
   </patient>
  </patients>
 </hospital>
</documents>

<!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=utf-8" />
<title>xml node read example in php</title>
</head>

<body>
 <h2>xml node read example in php</h2>
 <?php
  /*
  |--------------------------------------------------
  | create a file named patients2.xml &
  | cut and paste the above code to it.
  |--------------------------------------------------
  */
        $myXml = simplexml_load_file("patients2.xml") or die("Error: Cannot create object");
        print "
            <table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"40%\">
                <tr valign=\"top\" style=\"font-weight:bold; background:#aaa;\">
                    <td>Age</td>
                    <td>Name</td>
                    <td>Address</td>
                </tr>
        ";
        $bgColor = true;
        foreach($myXml->children() as $hospital)
        {
            foreach($hospital->children() as $patients)
            {
                foreach($patients->children() as $patient => $thisPatient)
                {
                    if($bgColor) 
                    {
                        $bgColor = false;
                        $color = "#aac";
                    }
                    else 
                    {
                        $bgColor = true;
                        $color = "#aab";
                    }
                    print "
                            <tr valign=\"top\" style=\"background:$color;\">
                                <td>$thisPatient->age</td>
                                <td>$thisPatient->name</td>
                                <td>$thisPatient->address</td>
                            </tr>
                    ";
                }
            }
        }
        print "
            </table>
        ";
    ?>
</body>
</html>


xml node read example in php output


No comments:

Post a Comment

leave your comments here..