xml string read

XmlStringExample.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 string example in php</title>
</head>

<body>
 <h3>xml string example in php</h3>
<?php
/*
|-------------------------------------------------
| assign the string into a variable
|-------------------------------------------------
*/
$xmlString =<<< END
<?xml version="1.0"?>
<patients> 
 <patient>
   <age>30</age>
   <name>cfsuman</name>     
   <address>mohakhali</address>
 </patient>
</patients>
END;
?>
 <?php
  /*
  |--------------------------------------------------
  | Interprets the string to an object using 
  | simplexml_load_string() & assign to a variable.
  |--------------------------------------------------
  */
  $xml = simplexml_load_string($xmlString) or die("Error: Can not create object");
  echo "<pre>";
  print_r($xml);
  echo "</pre>";
  /*
  |----------------------------------------------------------
  | $xml is a converted object and we can access like this :
  |----------------------------------------------------------
  */
 ?>
 <div style="height:30px; background:#aaa; color:pink; font-weight:bold; padding:5px; margin-bottom:1px;">
  <?=$xml->patient->name;?>
 </div>
 <div style="height:30px; background:#aab; color:orange; font-weight:bold; padding:5px;">
  <?=$xml->patient->address;?>
 </div>
</body>
</html>


xml string example in php output


No comments:

Post a Comment

leave your comments here..