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/>”;
}
}
?>



