zoukankan      html  css  js  c++  java
  • Php 解析XML文件

    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>


    效果图例如以下:


  • 相关阅读:
    使用Powershell开机启动隐藏窗口的程序
    使用鼠标左键事件实现VR中的Eye Gaze Input
    在github网站上更新fork的repo
    零Web知识个性化Blog
    C#中的Attribute
    ConsoleWindow中的双击日志定位
    Hackintosh Issues 10.13.x
    开启macOS的原生写入Ntfs的功能
    Install macOS High Sierra on Any Supported Intel-based PC
    DSDT/SSDT
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4208150.html
Copyright © 2011-2022 走看看