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

PHP 利用simplexml_load_file 读取XML对应的字段

时间:2016-04-22 16:12:01      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <news id="1" year="2015">
        <title>海口观澜湖华谊冯小刚电影公社南洋街盛大开街</title>
        <thumbnail>/news/2015/news-12-23_2.jpg</thumbnail>
        <images>
            <image>
                <source>/news/2015/news-12-23.jpg</source>
            </image>
            <image>
                <source>/news/2015/news-12-23_2.jpg</source>
            </image>
            <image>
                <source>/news/2015/news-12-23_3.jpg</source>
            </image>
            <image>
                <source>/news/2015/news-12-23_1.jpg</source>
            </image>
            <image>
                <source>/news/2015/news-12-23_4.jpg</source>
            </image>
        </images>

    </news>
    <news id="2" year="2015">
        <title>万圣节游玩攻略:深入观澜湖  变身“整鬼专家”</title>
        <thumbnail>/news/2015/1.png</thumbnail>
        <images>
            <image>
                <source>/news/2015/1.1.jpg</source>
            </image>
            <image>
                <source>/news/2015/1.2.png</source>
            </image>
            <image>
                <source>/news/2015/1.3.jpg</source>
            </image>
            <image>
                <source>/news/2015/1.4.jpg</source>
            </image>
            <image>
                <source>/news/2015/1.5.jpg</source>
            </image>
            <image>
                <source>/news/2015/1.6.jpg</source>
            </image>
            <image>
                <source>/news/2015/1.7.jpg</source>
            </image>
            <image>
                <source>/news/2015/1.8.png</source>
            </image>

        </images>
    </news>

</root>

 总体的思路是利用print_r 进行打印调试,遇到object就用->读取,遇到Array就用[]读取,在调试的时候遇到中文编码的问题用header("Content-Type: text/html; charset=UTF-8");

由于个人水平有限,调试花了一个多小时,希望看到这Blog的人少与半个小时调试出来。

<?php
$xml = simplexml_load_file("zh-cn/news.xml");
header("Content-Type: text/html; charset=UTF-8");
foreach ($xml as $news) {
    echo $news[‘id‘].‘<br/>‘;
    echo $news[‘year‘].‘<br/>‘;
    echo $news->title.‘<br/>‘;
    echo $news->thumbnail.‘<br/>‘;
    foreach ($news->images->image as $image) {
       echo $image->source.‘<br>‘;
    }
}
?>

 

PHP 利用simplexml_load_file 读取XML对应的字段

标签:

原文地址:http://www.cnblogs.com/Nick-Cai/p/5421328.html

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