Jump to content

Simple XML entry problem


Texan78
Go to solution Solved by Texan78,

Recommended Posts

Hello all, I have a very simple issue that I am having problems with. I have done this several times but not sure what I am missing. Probably just a brain fart and need a second set of eyes. 

 

I have a XML URL I am parsing with SimpleXML and for some reason it isn't pulling the data now that I have included it in a loop. Just trying to pull each entry and put it in its own table. This is something I have done over and over but I am missing something. 

 

Only listing the relevant code. The echo statement is further down the page and not included in the snippet. 

//Set path to data files

//Incidents XML URL
$incident_data = "http://dfwtraffic.dot.state.tx.us/DalTrans/GetFile.aspx?FileName=MapBurnerOutput/Incidents.rss&Exact=true";

//Lets load the XML file for the loop
$xml = simplexml_load_file($incident_data);

//Set initial output to false
$tData = false;
foreach($xml->channel as $channel)
{
    $incident_data_title = $channel->item['title'];
    $incident_data_desc  = $channel->item['description'];
    $incident_data_date  = $channel['pubDate'];
    $incident_data_updated = DATE("D, M d, g:i a", STRTOTIME($incident_data_date));

// construct data for table display
    $tData .= "<table style='{$tableStyle}' cellpadding='0' cellspacing='0'>\n";
    $tData .= "<tbody>\n";
    $tData .= "  <tr><td style='{$td1Style}'>Updated: {$incident_data_updated}</td></tr>\n";
    $tData .= "  <tr>\n";
    $tData .= "    <td style='{$td2Style}'><strong>Incident:</strong>{$incident_data_title}</td>\n";
    $tData .= "  </tr>\n";
    $tData .= "  <tr><td style='{$td3Style}'> </td></tr>\n";
    $tData .= "  <tr><td style='{$td4Style}'><strong>Description:</strong>{$incident_data_desc}</td></tr>\n";
    $tData .= "</tbody>\n";
    $tData .= "</table>\n";

}

Here is a sample of the XML file I am parsing. Very simple and not complicated. I can parse data outside the loop but, once I add it inside the loop it doesn't pull anything. 

 <?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:c2c="http://its.gov/c2c_icd" xmlns:tti="urn:tti">
  <channel>
    <title>Dallas Area Freeway Incidents
				</title>
    <link>http://dfwtraffic.dot.state.tx.us</link>
    <language>en-us</language>
    <category domain="dfwtraffic.dot.state.tx.us">LaneClosures</category>
    <lastBuildDate>5/11/2014 8:35:35 PM</lastBuildDate>
    <pubDate>5/11/2014 8:35:35 PM</pubDate>
    <image>
      <url>http://dfwtraffic.dot.state.tx.us/images/daltranslogo.gif</url>
      <link>http://dfwtraffic.dot.state.tx.us</link>
    </image>
    <item>
      <guid isPermaLink="false">200b64713c084791a12e34245791a575</guid>
      <title>VehicleCollision - Westbound IH 30 @ Westmoreland Rd</title>
      <link>http://dfwtraffic.dot.state.tx.us</link>
      <description><![CDATA[<div text-align:left;'><b>Status:</b> Verified at 8:31 PM on Sunday, May 11<br/><b>Affected Lanes:</b> Lane 1, Lane 2, Lane 3</div>]]></description>
    </item>
  </channel>
</rss>

What am I missing? 

 

-Thanks

Link to comment
Share on other sites

Title, description and pubDate are child elements, not attributes

foreach($xml->channel as $channel)
{
    $incident_data_title = $channel->item->title;
    $incident_data_desc  = $channel->item->description;
    $incident_data_date  = $channel->pubDate;
    $incident_data_updated = DATE("D, M d, g:i a", STRTOTIME($incident_data_date));
    
    echo 'Title: ' . $incident_data_title . '<br>';
    echo 'Desc: ' . $incident_data_desc . '<br>';
    echo 'Date: ' . $incident_data_date . '<br>';
}
Link to comment
Share on other sites

Thanks Barand, I realized that and got it sorted before I saw this. I was coming back to update it. Now the problem is, it's not looping through each entry, it only displays the first one. 

//Lane closure data
$lane_data = "http://dfwtraffic.dot.state.tx.us/DalTrans/GetFile.aspx?FileName=MapBurnerOutput/LaneClosures.rss&Exact=true";


//Lets load the XML file for the loop
$xml = simplexml_load_file($lane_data);

//Set initial output to false
$tData = false;
foreach($xml->channel as $channel)
{
    $incident_data_title = $channel->item->title;
    $incident_data_desc  = $channel->item->description;
    $incident_data_date  = $channel->pubDate;
    $incident_data_updated = DATE("D, M d, g:i a", STRTOTIME($incident_data_date));

// construct data for table display
    $tData .= "<table style='{$tableStyle}' cellpadding='0' cellspacing='0'>\n";
    $tData .= "<tbody>\n";
    $tData .= "  <tr><td style='{$td1Style}'>Updated: {$incident_data_updated}</td></tr>\n";
    $tData .= "  <tr>\n";
    $tData .= "    <td style='{$td2Style}'><strong>Roadway:</strong> {$incident_data_title}</td>\n";
    $tData .= "  </tr>\n";
    $tData .= "  <tr><td style='{$td3Style}'> </td></tr>\n";
    $tData .= "  <tr><td style='{$td4Style}'><strong>Description:</strong> {$incident_data_desc}</td></tr>\n";
    $tData .= "</tbody>\n";
    $tData .= "</table>\n";

}

echo $tData;

Link to comment
Share on other sites

You need to loop throught the items, not the channel

$xml = simplexml_load_file($lane_data);

$incident_data_date  = $channel->pubDate;
$incident_data_updated = DATE("D, M d, g:i a", STRTOTIME($incident_data_date));
foreach($xml->channel->item as $item)
{
    $incident_data_title = $item->title;
    $incident_data_desc  = $item->description;
    
    echo 'Title: ' . $incident_data_title . '<br>';
    echo 'Desc: ' . $incident_data_desc . '<br>';
    echo 'Date: ' . $incident_data_date . '<br>';
}
Link to comment
Share on other sites

  • Solution

Thanks that is what I thought. The issue I was fighting with that is when the data is outside the loop it doesn't pull the data. Not sure why as they doesn't need to be in it as you're right. Is this right?

//Lets load the XML file for the loop
$xml = simplexml_load_file($lane_data);

$incident_data_date = $channel->pubDate;
$incident_data_updated = DATE("D, M d, g:i A", STRTOTIME($incident_data_date));

//Set initial output to false
$tData = false;
foreach($xml->channel->item as $item)
{
    $incident_data_title = $item->title;
    $incident_data_desc  = $item->description;

// construct data for table display
    $tData .= "<table style='{$tableStyle}' cellpadding='0' cellspacing='0'>\n";
    $tData .= "<tbody>\n";
    $tData .= "  <tr><td style='{$td1Style}'>Updated: {$incident_data_updated}</td></tr>\n";
    $tData .= "  <tr>\n";
    $tData .= "    <td style='{$td2Style}'><strong>Roadway:</strong> {$incident_data_title}</td>\n";
    $tData .= "  </tr>\n";
    $tData .= "  <tr><td style='{$td3Style}'> </td></tr>\n";
    $tData .= "  <tr><td style='{$td4Style}'><strong>Description:</strong> {$incident_data_desc}</td></tr>\n";
    $tData .= "</tbody>\n";
    $tData .= "</table>\n";

}

echo $tData;

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.