码迷,mamicode.com
首页 > Web开发 > 详细

Php 解析XML文件

时间:2014-06-30 19:18:09      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:php   xml   遍历   html   

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>


效果图如下:

bubuko.com,布布扣

Php 解析XML文件,布布扣,bubuko.com

Php 解析XML文件

标签:php   xml   遍历   html   

原文地址:http://blog.csdn.net/yimiyuangguang/article/details/35823515

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!