Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php SimpleXML不返回任何内容_Php_Xml_Xml Parsing_Simplexml - Fatal编程技术网

Php SimpleXML不返回任何内容

Php SimpleXML不返回任何内容,php,xml,xml-parsing,simplexml,Php,Xml,Xml Parsing,Simplexml,我正在使用SimpleXML构建一个小脚本来解析一个小XML文件。我有几个使用SimpleXML的脚本,没有问题,所以我以前使用过它。所以我不知道为什么它不起作用。当我打印时($xml);测试它会输出元素,但我无法让它以其他方式解析数据 这是我正在使用的测试XML 下面是SimpleXML的片段 //Set path to data file $data = "http://www.mesquiteweather.net/xml/mesquite.xml"; // Lets parse th

我正在使用SimpleXML构建一个小脚本来解析一个小XML文件。我有几个使用SimpleXML的脚本,没有问题,所以我以前使用过它。所以我不知道为什么它不起作用。当我打印时($xml);测试它会输出元素,但我无法让它以其他方式解析数据

这是我正在使用的测试XML

下面是SimpleXML的片段

//Set path to data file
$data = "http://www.mesquiteweather.net/xml/mesquite.xml";

// Lets parse the XML feed
$xml = simplexml_load_file($data);

//Set initial output to false
    $tData = false;
foreach($xml->entry as $entry){

    $fname = $entry->fname;
    $lname = $entry->lname;
    $location = $entry->location;
    $report = $entry->report;
    $description = $entry->description;
如果有人需要在这里查看整个脚本,它就是

  <?php
#######################################################################################
#
#  MESQUITE STORM REPORTS
#  version 1.00
#
#  This program is free and no license is required.
#
#
#  mesquiteweather.net
#
#######################################################################################

////  SETTINGS  ////


////  SETTINGS  ////

$bkgColor                                       = '#d4d4d4';  // Background color   Examples:  "gray"  "#CCC"   "#CCCCCC"
$bc                                             = '#EEEEEE';  // Background color of table cells
$dtColor                                        = '#FFF';     // Date & time color  Examples:   "#FC0"   "#FFCC00"   "white"
$width                                          = '100%';     // Set the width of the report tables

////  END OF SETTINGS  ////


#######################################################################################

ini_set('display_errors','1');

## Start Configurable data ##

//Set path to data file
$data = "http://www.mesquiteweather.net/xml/mesquite.xml";

## End Configurable data ##

// overrides from the Carter Lake Settings.php file (if applicable)
global $SITE;
if(isset($SITE['cacheFileDir'])) {$cacheFileDir = $SITE['cacheFileDir']; }
if (isset($SITE['imagesDir']))   {$imagesDir = $SITE['imagesDir'];}
if(isset ($SITE['tz']))          {$ourTZ = $SITE['tz'];}
if(!function_exists('date_default_timezone_set'))
{
    putenv("TZ=" . $ourTZ);
}
else
{
    date_default_timezone_set("$ourTZ");
}

// get path info & protect for cross-site scripting vulnerability
$sri = ($_SERVER['REQUEST_URI']) ? str_replace('#SA', '', htmlspecialchars(strip_tags($_SERVER['REQUEST_URI']))) : '';

// set borders
   $bbrdr = 'border-bottom:thin solid black';       // bottom
   $lbrdr = 'border-left:thin solid black';         // left
   $rbrdr = 'border-right:thin solid black';        // right
   $tbrdr = 'border-top:thin solid black';          // top
   $sbrdr = 'border-right:thin solid black; '.
            'border-left:thin solid black';         // side

//Define table to display after each storm report
$afterTable = "<table style='margin-bottom: 5px;' border='0' cellpadding='0' cellspacing='0' width='100%'><tbody><tr><td><img alt='' src='images/1pixel.gif' border='0' height='7' width='7'></td><td class='shadow-mid' width='100%'><img alt='' src='images/1pixel.gif' border='0' height='7' width='7'></td><td><img alt='' src='images/1pixel.gif' border='0' height='7' width='7'></td></tr><tbody></table>\n";

// Let's assign the table some styles
   $noMessageStyle                              = "width:{$width}; text-align:center; margin:0px auto; background-color:{$bkgColor};";
   $td1Style                                    = "{$tbrdr};{$sbrdr}; padding:2px 0px 2px 6px;  background-image:url({$imagesDir}headerbgd2.gif); color:{$dtColor};";
   $td2Style                                    = "{$sbrdr}; padding:6px 0px 0px 6px;";
   $td3Style                                    = "{$sbrdr}; line-height:5px;";
   $td4Style                                    = "{$sbrdr}; {$bbrdr}; padding: 2px 6px 6px 6px;";


//Set message to display if there were not report
    $noStormMessage                                     .= "<table style='{$noMessageStyle}' cellpadding='0' cellspacing='0'>\n";
    $noStormMessage                                     .= "<tbody>\n";
    $noStormMessage                                     .= "  <tr><td style='{$td1Style}'>LIVE STORM REPORTS</td></tr>\n";
    $noStormMessage                                     .= "  <tr><td style='{$td4Style}'>There are currently no storm reports for Mesquite</td></tr>\n";
    $noStormMessage                                     .= "</tbody>\n";
    $noStormMessage                                     .= "</table>\n";
    $noStormMessage                                     .= $afterTable;

// Lets parse the XML feed
$xml = simplexml_load_file($data);

//Set initial output to false
    $tData = false;
foreach($xml->entry as $entry){

    $fname = $entry->fname;
    $lname = $entry->lname;
    $location = $entry->location;
    $report = $entry->report;
    $description = $entry->description;

// Set table style
   $tableStyle = "width: 100%; margin:0px auto; background-color:{$bkgColor};";
   $td1Style = "{$tbrdr};{$sbrdr}; padding:2px 0px 2px 6px;  background-image:url({$imagesDir}headerbgd2.gif); color:{$dtColor};";
   $td2Style = "{$sbrdr}; padding:6px 0px 0px 6px; background-color:{$reportColor};";
   $td3Style = "{$sbrdr}; line-height:5px; background-color:{$reportColor};";
   $td4Style = "{$sbrdr}; {$bbrdr}; padding: 2px 6px 6px 6px; background-color:{$reportColor};";


 // construct data for table display
    $tData .= "<table style='{$tableStyle}' cellpadding='0' cellspacing='0'>\n";
    $tData .= "<tbody>\n";
    $tData .= "  <tr><td style='{$td1Style}'><b>{$report}</b></td></tr>\n";
    $tData .= "  <tr>\n";
    $tData .= "    <td style='{$td2Style}'>Reported By: <b>{$fname} {$lname}</b>&nbsp;&nbsp; - &nbsp;&nbsp;</b>Location: <b>{$location}</b></td>\n";
    $tData .= "  </tr>\n";
    $tData .= "  <tr><td style='{$td3Style}'>&nbsp;</td></tr>\n";
    $tData .= "  <tr><td style='{$td4Style}'>Description: <b>{$description}</b></td></tr>\n";
    $tData .= "</tbody>\n";
    $tData .= "</table>\n";
    $tData .=  $afterTable;

 }

//If no storms were in the source, set no storm message
if(!$tData)
{
    $tData = $noStormMessage;
}

echo $tData;

?>

问题是
条目
不是
$xml
对象中的元素。它是根元素。因此,您不需要循环:

$fname = $xml->fname;
$lname = $xml->lname;
$location = $xml->location;
$report = $xml->report;
$description = $xml->description;

您可以在父元素中添加多个
条目
元素,这样原始代码就可以工作。

如果您链接到的XML文件确实是完整文件的代表,那么您的问题就在这里:
$XML->entry

SimpleXML没有表示文档的单独对象,加载XML字符串或文件时得到的对象是该文件的根元素。换句话说,
$xml
节点

所以你只需要说
$xml->fname
$xml->lname
,等等


一个XML文件只能有一个“根元素”,因此除非输入文件中的
外有一些容器,否则
foreach($XML->entry…
将无法工作。

我们可以看到打印结果吗?