Jump to content

Simple XML parsing problem


Texan78
Go to solution Solved by Texan78,

Recommended Posts

I am trying to parse an XML file which I have done many times but for some reason I am getting no output. Even when I try to echo a variable to test I get nothing.

 

Here is the test XML file I am using. Now it may be possible the XML is not formatted correctly but it should be.

 

http://www.mesquiteweather.net/xml/mesquite.xml

 

 

Here is the snippet to parse the XML

//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;

// 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>   -   </b>Location: <b>{$location}</b></td>\n";
    $tData .= "  </tr>\n";
    $tData .= "  <tr><td style='{$td3Style}'> </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;

?>

Here is the full code

<?php
#######################################################################################
#
#  SPOTTER NETWORK 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>   -   </b>Location: <b>{$location}</b></td>\n";
    $tData .= "  </tr>\n";
    $tData .= "  <tr><td style='{$td3Style}'> </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;

?>

What am I missing or overlooking?

 

-Thanks

Link to comment
Share on other sites

According to XML you will get values like this

$data = "http://www.mesquiteweather.net/xml/mesquite.xml";
$xml = simplexml_load_file($data);
$fname = $xml->fname;
$lname = $xml->lname;
$location = $xml->location;
$report = $xml->report;
$description = $xml->description;
Edited by pbs
Link to comment
Share on other sites

Actually, it *is* perfectly functional, if you print the data.

 

 

<?php

$data = "http://www.mesquiteweather.net/xml/mesquite.xml";
$xml = simplexml_load_file($data);

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

print ("First name: $fname\n");
print ("Last name: $lname\n");
print ("Location: $location\n");
print ("Report: $report\n");
print ("Description: $description\n");
?>

 

Result:

 

 

$ php -f weather.php
First name: First
Last name: Last
Location: Mesquite
Report: power
Description: Storm Report Power Outage

 

Your code assumes there's a wrapper around a bunch of entry elements, and there is not.

Link to comment
Share on other sites

There is no format that way, it just prints the raw data. That works for one single entry if you're not wrapping it in a table for data yes, but there will be multiple entries. This is just a test XML file with one entry to test and setup the script.

 

The variables are used in a table like so.

// 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>   -   </b>Location: <b>{$location}</b></td>\n";
    $tData .= "  </tr>\n";
    $tData .= "  <tr><td style='{$td3Style}'> </td></tr>\n";
    $tData .= "  <tr><td style='{$td4Style}'>Description: <b>{$description}</b></td></tr>\n";
    $tData .= "</tbody>\n";
    $tData .= "</table>\n";
    $tData .=  $afterTable;

Link to comment
Share on other sites

No one is stopping you from using the information that has been provided and massaging it into the format you desire.

 

If your XML file looked like this, your code would work.

 

 

<?xml version="1.0" encoding="utf-8"?>
<reports>
  <entry>
      <fname>First</fname>
      <lname>Last</lname>
      <location>Mesquite</location>
      <report>power</report>
      <description>Storm Report Power Outage</description>
  </entry>
  <entry>
      <fname>First Two</fname>
      <lname>Last Two</lname>
      <location>Mesquite</location>
      <report>lightning strike</report>
      <description>Lightning strike reported</description>
  </entry>
</reports>

 

 

$data = "http://www.mesquiteweather.net/xml/mesquite.xml";
$xml = simplexml_load_file($data);

foreach($xml->entry as $entry)
{
    $fname = $entry->fname;
    $lname = $entry->lname;
    $location = $entry->location;
    $report = $entry->report;
    $description = $entry->description;

    print ("First name: $fname\n");
    print ("Last name: $lname\n");
    print ("Location: $location\n");
    print ("Report: $report\n");
    print ("Description: $description\n");
    print ("\n");
}

 

Result:

 

 

$ php -f weather.php
First name: First
Last name: Last
Location: Mesquite
Report: power
Description: Storm Report Power Outage

First name: First Two
Last name: Last Two
Location: Mesquite
Report: lightning strike
Description: Lightning strike reported
 
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.