1.xml
<?xml version="1.0" encoding='utf-8' ?>
<root>
<items>
<name>虾米大王</name>
<age>22</age>
<job>计算机</job>
</items>
<items>
<name>虾米小王</name>
<age>21</age>
<job>网络管理</job>
</items>
</root>
2.php
<?php
/**
* Created by PhpStorm.
*/
class xml
{
var $m_char = "utf-8";
function __construct($ch = "utf-8")
{
$this->m_char = $ch;
}
function parse($xmlFile)
{
if(function_exists('simplexml_load_file'))
{
$xml = simplexml_load_file($xmlFile);
}
elseif(function_exists('file_get_contents'))
{
$xml = file_get_contents($xmlFile);
$xml = new SimpleXMLElement($xml);
}
elseif(function_exists('fopen'))
{
$handle = fopen($xmlFile,'r');
$xml = "";
while(!feof($handle))
{
$xml .= fread($handle,8192);
}
fclose($handle);
$xml = new SimpleXMLElement($xml);
}
else
{
$xml = false;
}
return $xml;
}
}
function xmlTable($content)
{
$header = "<tr><th>姓名</th><th>年龄</th><th>工作</th></tr>";
$body = "";
foreach ($content as $k => $v)
{
$body .= "<tr><td>".($v->name)."</td><td>".($v->age)."</td><td>"
.($v->job)."</td></tr>";
}
echo "<table border =1>".$header.$body."</table>";
}
function nodecodeTable($content)
{
$header = "<tr><th>姓名</th><th>年龄</th><th>工作</th></tr>";
$body = "";
foreach ($content as $k => $v)
{
$body .= "<tr><td>".($v->name)."</td><td>".($v->age)."</td><td>".($v->job)."</td></tr>";
}
echo "<table border='1'>".$header.$body."</table>";
}
$xFile = "1.xml";
$xml = new xml('gb2312');
$xml_content = $xml->parse($xFile);
xmlTable($xml_content);
nodecodeTable($xml_content);