简体   繁体   English

XML解析在PHP问题

[英]xml parsing in php issue

I have this code/function method as part of a class in php: 我有此代码/函数方法作为php中的类的一部分:

function defaulthome(){


    $fp = null;
    $err  ='';
    $xml_parser = xml_parser_create();    
    $rss_parser = new Rssparser();    
    xml_set_object($xml_parser,&$rss_parser);    
    xml_set_element_handler($xml_parser, "startElement", "endElement");    
    xml_set_character_data_handler($xml_parser, "characterData");    
    $fp = fopen("http://gulfnews.com/cmlink/business-rss-feed-1.446098?localLinksEnabled=false","r");  

      if(!$fp) $err = "Error reading RSS data.";  
      else {  

            $count = 0;

            while ($data = fread($fp, 4096) && $count<10) {  
                xml_parse($xml_parser, $data, feof($fp)) or $err=xml_error_string(xml_get_error_code($xml_parser)); 


                $count++;
            }


    }

    fclose($fp);    
    xml_parser_free($xml_parser);

    $content_sect2 = $this->tnjn->render('forms/landlords_prompt.phtml');
    $context = array('content1_title'=>'Welcome to my website','content1_article'=>"test article", 'feeds'=>$err);
    $output = $this->tnjn->render("default.phtml", $context);
    return $output; 

} }

I don't get results and the error i have is empty document! 我没有得到结果,并且我的错误是空文档! Does anyone know which part of the code is the problem? 有谁知道代码的哪一部分是问题所在?

Thanks very much !! 非常感谢 !!

See if turning error reporting on provides any information. 查看打开错误报告是否提供任何信息。 error_reporting(E_ALL);

Why are you trying to get The xml with fopen? 为什么要尝试用fopen获取xml? Instead try to get The xml with simplexml_load_file(); 相反,尝试使用simplexml_load_file();获取xml。

if (!$xml = simplexml_load_file("http://domain.com/rss.xml")) 
{
            echo('Unable to load or parse search results feed');
        }
        if (!count($entries = $xml->entry)) {
            echo('No entry found');
        }
        for($i=0;$i<count($entries);$i++) 
        {
            echo $entries->autor;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM