在PHP xmlparser中,为什么我不能从character_data_handler()存储全局?

时间:2010-03-02 20:39:45

标签: php global-variables xml-parsing

(或以下是代码要点:

$host = "";
...
xml_set_character_data_handler($xmlparser, "tagContents");
...
function tagContents($parser, $data) { 
    global $current; 
    global $host;
    if ($current == "HOST") { 
        $host = $data;         // Trying to store a global here
    }
    if ($current == "PATH") { 
        echo $host.$data;      // But its null when I get here.  WHY??
    }
}

我正在尝试将路径附加到此类主机以创建单行URL,因为xmlparse在每个回显之后放置一个换行符。所以换句话说,如果有人能告诉我如何防止换行,那也可以解决我的问题!

顺便说一下:

  • 我也尝试使用相同的结果引用超全局$ GLOBALS ['host']
  • 我的主机服务器(otherwise I'd use SimpleXML
  • 只提供PHP4

谢谢, 鲍勃

1 个答案:

答案 0 :(得分:0)

无论如何,尽量使用超全球$ GLOBALS ['host']。这是您的固定代码

$host = "";
...
xml_set_character_data_handler($xmlparser, "tagContents");
...
function tagContents($parser, $data) 
{ 
    global $current; 

    if ($current == "HOST") { 
        $GLOBALS['host'] = $data;         // Trying to store a global here
    }
    if ($current == "PATH") { 
        echo $GLOBALS['host'].$data;      
    }
}