# Getting certain number of entries from xml files

**URL:** <https://forum.kirupa.com/t/getting-certain-number-of-entries-from-xml-files/265039>\
**Category:** Uncategorized\
**Created:** [July 3, 2008, 8:40am UTC](https://forum.kirupa.com/t/getting-certain-number-of-entries-from-xml-files/265039 "2008-07-03T08:40:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaincaveman](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@captaincaveman](https://forum.kirupa.com/u/captaincaveman)\
**Post date:** [July 3, 2008, 8:40am UTC](https://forum.kirupa.com/t/getting-certain-number-of-entries-from-xml-files/265039/1 "2008-07-03T08:40:49Z")

</div>

Hi all,

Nice forum, been looking a few times on this site on various tutorials but never got round to registering till now when i came across a problem with the xml tutorial.

[http://www.kirupa.com/developer/php/php5\_simpleXML.htm](http://www.kirupa.com/developer/php/php5_simpleXML.htm)

Managed to get it working alright, however, im stuck on getting a page to display just the most recent 3 entries (or the top 3 entries) of the xml file.

I can get it to working without any extra tags or attributes in the xml file such as

\<news\>  
\<story\>some news item blah blah\</story\>  
\<story\>another news story\</story\>  
\</news

using the following php

```php
<?php
// set name of XML file
$file = "news.xml";
// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
// access each <item>
echo "<p>" . $xml->story[0] . "</p>";
echo "<p>" . $xml->story[1] . "</p>";
echo "<p>" . $xml->story[2] . "</p>";
?>

```

But im struggling with the tutorials xml example with more data involved in the xml file.

Any help would be appreciated

🙂

---

<div class="post-metadata">

**Author:** ![Charleh](https://avatars.discourse-cdn.com/v4/letter/c/a9a28c/32.png) [@Charleh](https://forum.kirupa.com/u/Charleh)\
**Post date:** [July 3, 2008, 8:57am UTC](https://forum.kirupa.com/t/getting-certain-number-of-entries-from-xml-files/265039/2 "2008-07-03T08:57:26Z")

</div>

Remember that your XML story nodes should contain child nodes which will be the story information (headline etc)

You can check the structure of your XML data after it’s loaded by using print\_r($xml)

So assuming you have the same XML file but you nest a ‘\<headline\>some headline\</headline\>’ element inside the story element you just need to access those parts in your code via the story object

$xml-\>story[0].headline[0]  
$xml-\>story[1].headline[0]

assuming that’s the way it’s been loaded

---

<div class="post-metadata">

**Author:** ![captaincaveman](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@captaincaveman](https://forum.kirupa.com/u/captaincaveman)\
**Post date:** [July 3, 2008, 9:40am UTC](https://forum.kirupa.com/t/getting-certain-number-of-entries-from-xml-files/265039/3 "2008-07-03T09:40:28Z")

</div>

the print\_r($xml) works, so the file contents are been called in.

Whatever i try though in displaying the information, nothing works, theres no errors, just nothing been displayed. So its not getting the information. I’ve tried all sorts of echo statements but all produce nothing.

---

<div class="post-metadata">

**Author:** ![Charleh](https://avatars.discourse-cdn.com/v4/letter/c/a9a28c/32.png) [@Charleh](https://forum.kirupa.com/u/Charleh)\
**Post date:** [July 3, 2008, 1:35pm UTC](https://forum.kirupa.com/t/getting-certain-number-of-entries-from-xml-files/265039/4 "2008-07-03T13:35:18Z")

</div>

Well the output that resides in each element of the array isn’t plain text - it’s a load of SimpleXMLElement objects - check the PHP documentation online

Try $xml-\>story[0]-\>headline or $xml-\>story[0]-\>headline[0]

Also try some of the other functions and see what you get

[http://uk.php.net/manual/en/ref.simplexml.php](http://uk.php.net/manual/en/ref.simplexml.php)

Can you post the full code/XML doc?

---

<div class="post-metadata">

**Author:** ![captaincaveman](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@captaincaveman](https://forum.kirupa.com/u/captaincaveman)\
**Post date:** [July 3, 2008, 3:08pm UTC](https://forum.kirupa.com/t/getting-certain-number-of-entries-from-xml-files/265039/5 "2008-07-03T15:08:05Z")

</div>

ahh…thanks…

$xml-\>story[0]-\>headline[0]

worked. Never thought about using more than one -\>, but now i do, i can see how it works

🙂
