XML Parser Using SimpleXMLImport

We seen already on lot of examples on parsing the XML element content to PHP. Here is the
below example will show you the method that parse the particular elements name and value of the XML as mentioned
like <var name=”" value=”" />.

For Example our XML look like this,

<?xml version=”1.0″ encoding=”UTF-8″?><?xml-stylesheet type=”text/xsl” href=”styles/status.xsl”?>
<status>
<group name=”cache”>
<var name=”rate” value=”87.03448016555201″/>
<var name=”misses” value=”544835″/>
<var name=”reqs” value=”4202184″/>
<var name=”reqs_sec” value=”115.68608466083175″/>
<var name=”messagesSent” value=”782394test”/>
</group>

from this reading the element which has the  var element tag property name as “messagesSent” and its value.

This done in the below PHP code. On running the code the PHP will parse the above XML data and display the particular tag specified value.

<?php

header(“Content-Type: text/xml”);
$dom = new domDocument;

$dom->load(‘status.xml’);

 if (!$dom) {
    echo ‘Error while parsing the document’;
    exit;
 }
 
 $s = simplexml_import_dom($dom);

for($i=0;$i<sizeof($s);$i++)
 {
 foreach($s->group[$i]->var as $gr)
  {
   if($gr['name']==’messagesSent’)
   echo $gr['name'].”–”.$gr['value'].”<br/>”;
  }
 }
?>

Simple JavaScript ‘Frame Busting’ Code

Here is a quick JavaScript  code that you can use if you want to make sure that no one should be able to show your website in an IFrame.

<script type=”text/javascript”>
if (top !=self) {
   top.location=self.location;
}
</script>

Just put this code in head section of your html page, and it will ensure that your page always get’s displayed outside the frames.

Follow

Get every new post delivered to your Inbox.