首页 > Web开发 > 详细

php SimpleXML 例子

时间:2016-11-24 18:02:21      阅读:129      评论:0      收藏:0      [点我收藏+]
$txt = GetRemoteText($url);
if(strlen($txt) > 0)
{
    $xml = simplexml_load_string($txt); //获取xml
    if($xml)
    {
        $logo = $xml->xpath("webinfo");
        $title = $xml->xpath("title");
        $keywords = $xml->xpath("keywords");
        $description = $xml->xpath("description");
        
        $ret .= "var WG_Logo = \"" . $logo[0]["logo"] . "\"\r\n";
        $ret .= "var WG_Title = \"" . $title[0]["title"] . "\"\r\n";
        $ret .= "var WG_Keywords = \"" . $keywords[0]["keywords"] . "\"\r\n";
        $ret .= "var WG_Description = \"" . $description[0]["description"] . "\"\r\n";

        $group_info = "\r\nvar group_info = [\r\n";
        $district_info = "\r\nvar district_info = [\r\n";
        foreach ($xml->children() as $child)
        {
            if("gamelist" == $child->getName())
            {
                //组信息
                foreach($child->children() as $groupChild)
                {
                    $group_info .= "[‘" . $groupChild["id"] . "‘,‘" . $groupChild["gamename"] . "‘],\r\n";
                }
            }
            else if("zonelist" == $child->getName())
            {
                //分区信息
                foreach($child->children() as $districtChild)
                {
                    $district_info .= "[‘" . $districtChild["id"] . "‘,‘" . $districtChild["zonename"]
                         . "‘,‘" . $districtChild["starttime"] . "‘,‘" . $districtChild["gameid"]. "‘],\r\n";
                }
            }
        }
        $group_info .= "];\r\n";
        $district_info .= "];\r\n";
        $ret .= $group_info . $district_info;
        
        $file = fopen($localJsPath, "w");
        fwrite($file, $ret);
        fclose($file);
    }
}
$xml->xpath("webinfo")[0]["logo"] 有些php编译器通不过

php SimpleXML 例子

原文:http://www.cnblogs.com/barrysgy/p/6098719.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!