php SimpleXML 例子

Posted 悉野小楼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php SimpleXML 例子相关的知识,希望对你有一定的参考价值。

$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 例子的主要内容,如果未能解决你的问题,请参考以下文章

PHP 用于Twitter搜索的SimpleXML代码

PHP 5 SimpleXML 函数

PHP 5 SimpleXML 函数

雷林鹏分享:PHP SimpleXML

使用 $variable 作为索引取消设置 PHP SIMpleXML 节点

缺少时间导致的 PHP SimpleXML 错误