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

php SimpleXML 例子

时间:2016-11-24 18:02:28      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:cal   for   simple   ret   fopen   tar   var   time   one   

$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 例子

标签:cal   for   simple   ret   fopen   tar   var   time   one   

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

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