Php 解析XML文件
Php 解析XML文件,仅供学习參考!演示样例代码例如以下:
<?php
header("Content-type: text/html; charset=utf-8");
if(!isset($_GET["_u"])){
echo "<script>javascript:alert('请勿非法訪问!');window.history.back();</script>";
exit();
} $url = $_GET["_u"];
?>
<table width="100%" height="auto" cellpadding="0" cellspacing="0">
<tbody>
<?php
//创建Document对象
$doc = new DOMDocument();
$doc -> load($url); $news = $doc -> getElementsByTagName("item"); $title = "";$date = "";$detail = "";
$len = ($news -> length);$model = null; if($len > 0){
//遍历子节点
for ($i=0; $i < $len; $i++) {
$model = $news -> item($i);
$title = $model -> getElementsByTagName("title") -> item(0) -> nodeValue;
$date = $model -> getElementsByTagName("pubDate") -> item(0) -> nodeValue;
$detail = $model -> getElementsByTagName("content") -> item(0) -> nodeValue;
?>
<tr>
<th>
<a href="javascript:void(0);"><?php echo($title) ?></a>
<a href="javascript:void(0);"><img alt="" src="/images/Plus.jpg" title="展开" /></a>
</th>
<th>
<?php echo($date) ?>
</th>
</tr>
<tr style="display:none;">
<th colspan="2">
<?php echo($detail) ?>
</th>
</tr>
<?php
}
}else{
//没有内容
?>
<tr>
<th colspan="2">
<b><font color="red">临时没有内容显示!</font></b>
</th>
</tr>
<?php
}
?>
</tbody>
</table>
效果图例如以下: