Problem with PHP/XML Script

Hi there,

I am writing a script that will feed data directly from an XML source. I have managed to get the script to work but I’m having trouble importing one of the data fields properly and I’m looking for a bit of insight. I’ll break it down for you.

The XML Data (A yearly horoscope report) is structured like so:

<xml>
<forecast_2011>
<overview>
<detail>
<paragraph id="1">content </paragraph>
<paragraph id="2">content </paragraph>
<paragraph id="3">content </paragraph>
<paragraph id="4">content </paragraph>

I’m using PHP to reference the feed and pull the 4 paragraphs that I need. But for some reason, I can only get the first paragraph (ID=“1”) to show.

When I use the following code:

<?php echo $xml->forecast_2011->overview->detail->paragraph; ?>

I get the first paragraph (ID=“1”) but that’s all.

I have tried changing this to all sorts. I’ve tried adding ID=“1” through to ID=“4” but none of them produce anything. In fact, if I add anything to the end of my string, it doesn’t produce any results at all, not even the first paragraph. I’ve tried it with spaces, without spaces, with speech marks, without speech marks, with brackets, with curly brackets, with square brackets… I cannot think of any other way of attaching the required attribute so my script knows to pull ID=“2” or ID=“3” instead of just bringing up the first instance of the referenced element.

So my question is… how do I correctly call an XML Element dependant on the ID Attribute it has been given instead of just calling the first instance of the said element?

$xml->forecast_2011->overview->detail->paragraph is an object. It has an attribute named “id”. And a subscript named [0].
Try, for instance:

echo $xml->forecast_2011->overview->detail->paragraph[2][0];

and you’ll get its content.

Try

$arr=$xml->forecast_2011->overview->detail->paragraph[2]->attributes();
echo $arr["id"];

and get the value of the attribute “id”.

SimpleXML is not very simple…

Hi there Amit,

Tried both of those and neither produced any results. Maybe I’ve missed something in your reply.

Here’s my full PHP file - Please can you point out what I need to change in order to make it work?

<?php

$year = date ('Y');

$file = 'http://www.zdki.us/taReportsw/YearlyForecast.aspx?SunSign=Aquarius&Year=' .urlencode($year) . '&ReportFormat=XML&ReportVariation=ext&AccountID=Neosis&AppID=CDS&MemberID=1234567890&V=2';

$xml = simplexml_load_file($file);


?>

<?php echo $xml->forecast_2011->overview->detail->paragraph; ?>

I tried loading it from a string using SimpleXMLElement and it worked fine.

<?php
error_reporting(-1);
ini_set('display_errors', true);

$feed = new SimpleXMLElement(
  'http://www.zdki.us/taReportsw/YearlyForecast.aspx?SunSign=Aquarius&Year=2011&ReportFormat=XML&ReportVariation=ext&AccountID=Neosis&AppID=CDS&MemberID=1234567890&V=2',
  null,
  true
);
?>
<h1>
  <?php echo $feed->forecast_2011->overview['sign']; ?>
</h1>

<h3>
  <?php echo $feed->forecast_2011->love['title']; ?>
</h3>
<?php foreach($feed->forecast_2011->love->detail->paragraph as $paragraph): ?>
  <p>
    <?php echo $paragraph; ?>
  </p>
<?php endforeach; ?>

<h3>
  <?php echo $feed->forecast_2011->career['title']; ?>
</h3>
<?php foreach($feed->forecast_2011->career->detail->paragraph as $paragraph): ?>
<p>
  <?php echo $paragraph; ?>
</p>
<?php endforeach; ?>

Thank you so much Anthony - Your method worked perfectly!

Thanks to you too Amit for your guidance :slight_smile:

Chris