How To Read Xml and Convert To Html Via Php Code

Vu Old Solved Programming Based Assignments Codes are Available in this Section.
Post Reply
User avatar
ALI RAZA
★★★★★★★★★★★★★
★★★★★★★★★★★★★
Posts: 177
Joined: Fri Dec 22, 2017 8:10 am
Location: Lahore
Has thanked: 6 times
Been thanked: 7 times
Contact:
Status: Offline

How To Read Xml and Convert To Html Via Php Code

Post by ALI RAZA » Sun Sep 30, 2018 11:28 am

Well-formed code of XML is given below which is showing projects detail in XML format. You are required to write PHP code which will read given XML code and display in HTML table as showing in screen shot of sample output.

<?xml version='1.0' encoding='UTF-8'?>
<Projects_Detail>

<Mr-Imran>
<projectBudget>125000</projectBudget>
<EmpNo>E100</EmpNo>
<DeptName>IT</DeptName>
<HourlyRate>625</HourlyRate>
</Mr-Imran>

<Mr-Imran>
<projectBudget>120000</projectBudget>
<EmpNo>E101</EmpNo>
<DeptName>Database</DeptName>
<HourlyRate>500</HourlyRate>
</Mr-Imran>

<Mr-Imran>
<projectBudget>135000</projectBudget>
<EmpNo>E102</EmpNo>
<DeptName>IT</DeptName>
<HourlyRate>550</HourlyRate>
</Mr-Imran>

<Mr-Imran>
<projectBudget>115000</projectBudget>
<EmpNo>E103</EmpNo>
<DeptName>Testing</DeptName>
<HourlyRate>400</HourlyRate>
</Mr-Imran>

<Mr-Imran>
<projectBudget>150000</projectBudget>
<EmpNo>E110</EmpNo>
<DeptName>Database</DeptName>
<HourlyRate>375</HourlyRate>
</Mr-Imran>

<Mr-Imran>
<projectBudget>140000</projectBudget>
<EmpNo>E112</EmpNo>
<DeptName>Testing</DeptName>
<HourlyRate>425</HourlyRate>
</Mr-Imran>

</Projects_Detail>
Image


Answer : php code

Code: Select all

<?php
$data ="<?xml version='1.0' encoding='UTF-8'?>
<Projects_Detail>

    <Mr-Imran>
        <projectBudget>125000</projectBudget>
	<EmpNo>E100</EmpNo>
	<DeptName>IT</DeptName>
	<HourlyRate>625</HourlyRate>
    </Mr-Imran>

    <Mr-Imran>
        <projectBudget>120000</projectBudget>
	<EmpNo>E101</EmpNo>
	<DeptName>Database</DeptName>
	<HourlyRate>500</HourlyRate>s
    </Mr-Imran>

    <Mr-Imran>
        <projectBudget>135000</projectBudget>
	<EmpNo>E102</EmpNo>
	<DeptName>IT</DeptName>
	<HourlyRate>550</HourlyRate>
    </Mr-Imran>

    <Mr-Imran>
        <projectBudget>115000</projectBudget>
	<EmpNo>E103</EmpNo>
	<DeptName>Testing</DeptName>
	<HourlyRate>400</HourlyRate>
    </Mr-Imran>

    <Mr-Imran>
        <projectBudget>150000</projectBudget>
	<EmpNo>E110</EmpNo>
	<DeptName>Database</DeptName>
	<HourlyRate>375</HourlyRate>
    </Mr-Imran>

    <Mr-Imran>
        <projectBudget>140000</projectBudget>
	<EmpNo>E112</EmpNo>
	<DeptName>Testing</DeptName>
	<HourlyRate>425</HourlyRate>
    </Mr-Imran>
    
</Projects_Detail>";

$xml_object = simplexml_load_string($data) or die("Error: Cannot create object");
$xml_array=(array)$xml_object; 
echo "<table border='1' cellpadding='10' cellspacing='0' align='center'>  
    <caption><h2>".$xml_object->getName()."</h2></caption> 
    <tr>
        <th>Project No</th>
        <th>Project Manager</th> 
        <th>Project Budget</th> 
        <th>Employee Name</th> 
        <th>Deptartment Name</th> 
        <th>Hourly Rate</th> 
    </tr>";

$serialNo = 0;
foreach($xml_array as $key1 => $value1){   //$key1 is name of element
	
	foreach($value1 as $key2 => $value2){  //$key2 is index of child element

		$serialNo++;
		echo "<tr><td>".$serialNo."</td><td>".$key1."</td>";
		
		foreach($value2 as $key3 => $value3){  // $key3 name of child element and $value3 is text of chalid element
			echo "<td>".$value3."</td>";
		}
		echo "</tr>";
		
	}
}

echo "</table>";
?>



Post Reply

Last 10 Members Who Visited This Topic

  • Similar Topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 1 guest